C++ Specific File Handling

Viewed 30

I need help in my program where I need to extract the first line of an input txt file that is filled with numbers separated by commas that need to be put into a singly linked list.(Ex: 6,3,5,7) But also the rest of the line has specific instructions and numbers that are separated by a colon.(Example: insert at top:3). The instruction will be used to change the l.list by what asking. The struggling with is how would i be able to extract the information properly. In the program, I can't use advanced structures like trees or hash. Also, I can only use a vector for the input only. I have managed to get a way to get the first line but the second line is what gives most trouble. I have tried to use it to get lines so it can maybe ignore the first line to get the second line but it didn't work.

Here is an example text file: 0,98,6,7,43 insert at top:10 insert at bottom:20 insert at position 1:73 delete at position 4 reverse print middle keep unique

Here is what I have right now for the source code:

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

using namespace std;

struct numList
{
  int data
  string instruction;
  numList*next;
};

class node()
{
  private:
  numList*head = nullptr;
  numList*tail = nullptr;
  public:
}

int main() {
  ifstream inFile;
  ofstream outFile;

  // Un-hardcode this part bellow

  inFile.open("input1.txt");
  outFile.open("output.txt");

  if (inFile.is_open()) 
  {
    string line;
    getline(inFile, line);
    stringstream ss(line);

    vector<int> inNumbers;

    string number;
    while (getline(ss, number, ',')) 
    {
      int _number = stoi(number);
      inNumbers.push_back(_number);
      
    }
    ss.clear();

    line.ignore(30,'/n');
    stringstream ss(line);
    
    vector<string>instuctions;

    while(getline(ss,line,':'))
    {
      instuctions.pushback(line);
    }

    
  }

  inFile.close();
  outFile.close();
}

I just want to know a way I can try to get the second please explain how your concept or code works so I am not copying. I will respond later to the answer since it is later where I am and I am gonna sleep.

0 Answers
Related