Isn’t it possible to open two files simultaneously using different ofstreams? What I am trying to is writing to two ofstreams, one has a variable filename which changes every time the loop iterates, the other has a fixed filename and the data I am writing on is to be appended at every iteration of the loop. To demonstrate:
ofstream file_variable_name;
ofstream file_to_be_appended;
{ //THIS IS A LOOP, variable_name changes at every iteration
file_variable_name.open(variable_name.c_str(), ios::out);
file_to_be_appended.open("fixed name", ios::out | ios::app);
//Do lots of things here, make data ready to be written to file
file_variable_name << "write something" << endl;
file_to_be_appended << "write same as above, but this is to be appended" << endl;
file_variable_name.close();
file_to_be_appended.close();
}
Somehow, I could not even manage to get the second file to be created let alone opened and appended. I can send the full code (it’s around 1000 lines or so, needs be truncated), as well, but I thought the above would explain what I am trying to do, and any logic flaws would be apparent to pros.
Thanks in advance for all suggestions!
You’re making it far harder than it is. Generally there’s no need to use .open() and .close() methods, and you don’t need to keep reopening the same file: