By default, opening a file for output will truncate the it, overwriting its contents. std::ostream::open
can take two arguments, the latter of which can be used to tell it to append data to the file, instead of this default behaviour. See this page for more details on what std::ios::out
and std::ios::app
are doing in the following example.
So, your program might look more like this:
void csv_export1(const int &score) {
std::ofstream my_file;
my_file.open("score.csv", std::ios::out | std::ios::app);
my_file << "USERNAME" << "," << score << std::endl;
}
On another note, as has been mentioned elsewhere, this approach of opening the file each time the function is called can be costly. A more efficient approach might be to open the file elsewhere, and pass it to the function by reference.