Search code examples
c++ostream

Write to file in a loop cpp


A general question of writing to files in a loop and outside a loop:

suppose I have the following code:

# include <iostream>
# include <string>
# include <fstream>
# include <sstream>

using namespace std;
void printToFile(string name, double **Array) 
{
    ofstream myfile;
    myfile.open(name);
    for (unsigned int m=0; m<5; m++)
    {
        for (unsigned int n=0; n<10; n++)
        {
            myfile << Array[m][n] << " ";
        }
    }
    myfile.close();
}

int main()
{
    int i,j;
    double **Arr;
    string str;
    ostringstream oss;

    for (i=0; i<5; i++)
    {           
        for (j=0; j<10; j++)
        {
            // some calculation that yields Arr with size [5][10]
            str="";
            oss << "Arr_j_"<<j+1<<"_i_"<<i+1<<".txt";
            str = oss.str();
            printToFile(str,Arr);
        } 
    } 
    return 0;
}

I can't seem to get the name of the files right, what I get is:

Arr_j_1_i_1.txtArr_j_2_i_1.txtArr... etc

Also, my code run finish but the output files stop at i=2, I would also appreciate comments about which steam to use (ostream,fstream,sstream, etc...)


Solution

  • You are never clearing out the ostringstream after each iteration. To do that automatically for you, you can just move the oss declaration into the loop. This way after each loop the stream will be destroyed and you will start with a fresh stream on the next iteration.

    for (i=0; i<5; i++)
    {           
        for (j=0; j<10; j++)
        {
            ostringstream oss;
            // some calculation that yields Arr with size [5][10]
            oss << "Arr_j_"<<j+1<<"_i_"<<i+1<<".txt";
            printToFile(oss.str(),Arr);
        } 
    } 
    

    I also removed str="" and str = oss.str(); as it is not needed. You can just use oss.str() as a parameter to printToFile and get rid of str.

    I am not sure if it would be faster or not but you could avoid using any type of stringstream and construct the string in the function call using std::to_string to convert the indexes into strings. that would look like

    for (i=0; i<5; i++)
    {           
        for (j=0; j<10; j++)
        {
            // some calculation that yields Arr with size [5][10]
            printToFile("Arr_j_" + std::to_string(j+1) + "_i_" + std::to_string(i+1) + ".txt", Arr);
        } 
    }