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();