How to append text to a text file in C++? And create a new text file if it does not already exist and append text to it if it does exist.
You need to specify the append open mode like
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app); // append instead of overwrite
outfile << "Data";
return 0;
}