1

I currently have this to write to a file in my program. I have all the variables ready to put into the file I just have an issue.

void fileWrite(int score)
{
    ofstream file;
    file.open("MathGen.txt");
    file << "The score was: " << score;
    file.close();
}

So when this runs it creates the files and writes the score perfectly fine. But if I re-run the program and get a new score it will overwrite the old score. Is there any way to stop this from happening? I know in python you could use file write functions such as "a+". But that doesn't work here.

bytecode77
  • 14,163
  • 30
  • 110
  • 141
Reece
  • 23
  • 6

1 Answers1

2

You should use append mode on your file in ofstream.open:

file.open("MathGen.txt", std::ofstream::app);

More info on this method is here

bytecode77
  • 14,163
  • 30
  • 110
  • 141