开发者

C++ ofstream not operating as expected

I have a feeling that I'm missing something obvious but can't seem to resolve the following:

Please see the code below. All it does is loop 20 times and write the index of a loop to a file. After every three writes to the file that file is closed and a new one started (using an IF loop that checks for the 3 writes.

It works fine for the first file and writes as expected. Then the IF kicks in for the first time and the current file is closed and a new one started.

NOTE: at this point I can write to the new files fine.

Then the IF ends and drops processing back to the FOR loop. But now writing to the file does not work (no errors, nothing written).

So the file is written to with success in the IF block that creates it. Then the IF block ends. Then the FOR loop ends the current pass and starts a new pass. Now writing does not work.

Can anyone help, I can find ways to do what I need to do differently but I just want to understand why this is happening?

int main()
{
    unsigned int test_rowcounter = 0;
    unsigned int test_filenumber = 0;
    char filenamebuilder[50] = ""; 

    sprintf(filenamebuilder, "testing_file%d",test_filenumber);
    strcat(filenamebuilder,".tsv"); 
    ofstream fsTestOutput;
 开发者_开发问答   fsTestOutput.open(filenamebuilder, fstream::out);

    //Loop 20 times writibng the loop index value to a file
    for(unsigned int f=0;f<20;f++)
    {               
        fsTestOutput << f; //This write only works for the original file
        test_rowcounter++;

        //If three rows have been written to a file then start a new file
        if(test_rowcounter == 3)
        {
            test_rowcounter = 0; 
            test_filenumber++; // increment the number that is added to the 
                               // base filename to create a new file

            //Close the previous file and open a new one
            fsTestOutput.close();           
            sprintf(filenamebuilder, "testing_file%d",test_filenumber);
            strcat(filenamebuilder,".tsv"); 
            ofstream fsTestOutput;
            fsTestOutput.open(filenamebuilder, fstream::out);
            // This next line works, the data is written 
            // for all files at this point
            fsTestOutput << "FILE JUST CHANGED,";
        }
    }
}


You're declaring a second ofstream fsTestOutput in your if statement when you create the new file.

That second ofstream has scope local to your if statement, so it'll work fine within your if. However, when you leave the if statement and head back to the for loop, the new ofstream instance goes out of scope, and your code reverts back to using the original one (which you've closed, hence no output!)

  if(test_rowcounter == 3)
  {
        test_rowcounter = 0; 
        test_filenumber++; 
        //Close the previous file and open a new one
        fsTestOutput.close();           
        sprintf(filenamebuilder, "testing_file%d",test_filenumber);
        strcat(filenamebuilder,".tsv"); 
        ofstream fsTestOutput;    // GET RID OF THIS
        fsTestOutput.open(filenamebuilder, fstream::out);
        fsTestOutput << "FILE JUST CHANGED,";
  } 


Try removing the ofstream fsTestOutput; line from within the loop, basically you are creating an ofstream in the wrong scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜