Faced such a problem: there is already data in the file and new ones need to be written to it, while the old ones are not deleted. In my case, the old data is deleted and a new line is written instead.

#include <fstream> ... ofstream fout; fout.open("file.txt"); ... fout << "abcd"; fout.close(); 

    1 answer 1

    The open method has a second parameter that determines how to work with the file. You need to set the app mode - write to the end:

     open("file.txt", ios_base::out | ios_base::app);