Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help to write a C++ code version of this program import java.io.*; import j

ID: 3599249 • Letter: N

Question

need help to write a C++ code version of this program
import java.io.*;
import java.util.LinkedList;

public class Packet4 {


public static void main(String[] args) throws IOException
{
  
  File dataFile = new File("data.txt");  
  FileInputStream stream = new FileInputStream(dataFile);  
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

  String line = null;
  
  
  LinkedList<String> packetsList = new LinkedList<>();  
  
  
  while ((line = reader.readLine()) != null)
  {            
   
   packetsList.add(line);   
  }
  
  int nextExpected = 1; // the lowest sequence number in data.txt is 1
  String previousPacketData = "";
  
  
  while (nextExpected <= packetsList.size())
  {
   
   for (int i = 0; i < packetsList.size(); i++)
   {
    String packet = packetsList.get(i);
    
    String sequenceNumberStr = packet.substring(0,4);
    
    
    sequenceNumberStr = sequenceNumberStr.trim();     
    
    
    int sequenceNumberInt = Integer.parseInt(sequenceNumberStr);
    
    
    if (nextExpected == sequenceNumberInt)
    {
     String packetData = packet.substring(4,16);
     packetData = packetData.replace("=+", " ");
     
     
     if (previousPacketData.endsWith("=") && packetData.startsWith("+"))
     {
      previousPacketData = previousPacketData.replace("=", "");
      packetData = packetData.replace("+", " ");
     }
     
     System.out.print(previousPacketData); // print the previous packet data
     
     
     previousPacketData = packetData;    
     
     nextExpected = nextExpected + 1; // increment next-expected counter
    }
   }
  }
  
  System.out.print(previousPacketData); // print the last piece

  reader.close();
}
}

Explanation / Answer

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>
#include <sstream>

using namespace std;

string trim(string const& str)
{
string word;
stringstream stream(str);
stream >> word;

return word;
}

string replace(string str, string word, string repl)
{
int index = str.find(word);
if (index != -1)
str.replace(index, word.size(), repl);
return str;
}

int main()
{

ifstream in("data.txt");
string line;

vector<string> packetsList;

while (getline(cin, line)) {

packetsList.push_back(line);
}

int nextExpected = 1; // the lowest sequence number in data.txt is 1
string previousPacketData = "";

while (nextExpected <= packetsList.size()) {

for (int i = 0; i < packetsList.size(); i++) {
string packet = packetsList[i];

string sequenceNumberStr = packet.substr(0, 4);

sequenceNumberStr = trim(sequenceNumberStr);

int sequenceNumberInt = atoi(sequenceNumberStr.c_str());

if (nextExpected == sequenceNumberInt) {
string packetData = packet.substr(4, 16);
packetData = replace(packetData, "+", " ");

if (previousPacketData[previousPacketData.size() - 1] == '=' && packetData[0] == '+') {
previousPacketData = replace(previousPacketData, "=", "");
packetData = replace(packetData, "+", " ");
}

cout << previousPacketData; // print the previous packet data

previousPacketData = packetData;

nextExpected = nextExpected + 1; // increment next-expected counter
}
}
}

cout << previousPacketData; // print the last piece

in.close();
}