I am trying to write some code that is going to output a speed and time values to multiple files, specifically 4 different files which represents 4 different objects moving.
I know that to open and write to 1 file I can do the following:
#include <iostream>
#include <fstream>
std::ofstream outfile ("test.txt");
outfile << "my text here!" << std::endl;
outfile.close();
However I need to adapt this such that it would work in a switch statement as follows:
for (int i = 0; i < numRobots; i++){
switch (i){
case 0:
if (r1_stop == 1) r1Speed = 0;
else //Update speed
r1File << r1Speed << " " << time << endl;
case 1:
if (r2_stop == 1) r2Speed = 0;
else //Update speed
r2File << r2Speed << " " << time << endl;
case 2:
if (r3_stop == 1) r3Speed = 0;
else //Update speed
r3File << r3Speed << " " << time << endl;
case 3:
if (r4_stop == 1) r4Speed = 0;
else //Update speed
r4File << r4Speed << " " << time << endl;
}
}
Where r1File, r2File, r3File, and r4File are the files containing the speed and time for the respective objects. Im not quite sure how to implement this type where I have multiple files open or do i have to keep opening and closing the files? Im worried about overwriting existing data in the file if that is the case unless it knows to not start from the beginning of the file when opening it again.
Any help is appreciated, thanks