Search code examples
c++ofstream

Opening/Writing to multiple files


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


Solution

  • By default std::ofstream overwrites instead of appends, so from the moment you open the file it will be streaming bytes in that overwrite whatever was there before it was opened.

    *fstream variants keep a file open until the stream object is destroyed. In other words a file being open is tied to the lifetime of the *fstream object representing it. Once destroyed, the file is immediately closed. This concept is known as RAII. In your case the stream objects are global, and thus are destroyed after main() ends, right before the application terminates.

    If writing to the files isn't time critical, your implementation is good enough. On the other hand if you need more accurate measurements, consider writing each of your outputs to an intermediate buffer such as std::stringstream, then stream that data to your files after you're done taking measurements.

    However, if you don't need the data from previous application runs, don't bother using files and instead just write to memory using a std::stringstream.