Save to file - Permanent while loop

Hello forum,

I am trying to write some data into a file in a while loop as shown below. The problem is, myfile.open keeps getting called and hence the text file only has 1 line of sentence as opposed to many.

while(1){
static ofstream myfile;
static char filename[40];
sprintf(filename, "Test%d.txt", TID);
myfile.open(filename);
myfile << "Writing this to a file.\n";
}

is there a static myfile.open(filename) so that I will only call it once ? I CANNOT change the format of the code. I just need help figuring out how to not reopen the file with every loop iteration. Help thanks.
static std::ofstream my_file( "Test" + std::to_string(TID) + ".txt" ) ; // #include <string>
Topic archived. No new replies allowed.