Output file is not the same as input file

Hi all,

I have a school assignment for which I have to create a Lexical Analyzer in C++. Up until now I've had only a few small problems.

However, I've come across a problem which totally caught me by suprise.

This is what is happening:

I have a class in which I read input from a .txt file named input.txt.

This is the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FileReader::FileReader()
{
}
string FileReader::ReadFile()
{
   string line,input;
  ifstream myfile ("input.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      input+= line;
    }
    myfile.close();
  }

  else
  { 
      cout << "Unable to open file";
  }
  
  return input;
                         
}


Then I use the returned input in a different class to get some info from it.
Most important is the fact I need to get the '\n' char to know how many lines the file has.

Then I use a vector to add strings I want to output in an output file named "output.txt"

This is the class I use to output the vector into the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FileWriter::FileWriter()
{
}

void FileWriter::WriteToFile(vector<string> output)
{
  ofstream myfile;
  myfile.open ("output.txt");
  for(int i =0; i<output.size();i++)
  {
      myfile << output.at(i);
  }
  myfile.close();
}


Now comes the weird thing. All of the classes are quite simple except the one I use to extract info fromt he string. However that class is irrelavant for now.

If I use an input file("input.txt") which looks like this:
1
2
1:test test test{}
2:test test test{}


It is put into the ouput file("output.txt") like this:
1:test test test{}2:test test test{}

So it's like the input is read but no '\n' char is read. How do I insure that I get an '\n' char?

Greetings Rope.

Thanks in advance.


Woops.. I just figured it out couldn't be simpler. Just +='ed an '\n' to input string in my FileReader class.. Thanks for reading anyway.
Last edited on
Topic archived. No new replies allowed.