C++ ofstream 未按预期运行

发布于 2024-11-02 14:07:28 字数 1703 浏览 0 评论 0原文

我有一种感觉,我错过了一些明显的东西,但似乎无法解决以下问题:

请参阅下面的代码。它所做的只是循环 20 次并将循环索引写入文件。每对文件进行三次写入后,该文件将被关闭并启动一个新文件(使用 IF 循环来检查 3 次写入。

它对于第一个文件工作正常并按预期写入。然后 IF 第一次启动并当前文件已关闭,新文件已启动。

注意:此时我可以正常写入新文件,

然后 IF 结束并将处理返回到 FOR 循环,但现在写入文件不起作用(没有错误)。 也没写)。

,什么 成功写入,然后 FOR 循环结束当前传递并开始新的传递。

在创建它的 IF 块中 需要采取不同的做法,但我只是想了解为什么会发生这种情况?

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,";
        }
    }
}

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,";
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-11-09 14:07:28

创建新文件时,您将在 if 语句中声明第二个 ofstream fsTestOutput

第二个 ofstream 的作用域仅限于您的 if 语句,因此它可以在您的 if 中正常工作。但是,当您离开 if 语句并返回到 for 循环时,新的 ofstream 实例将超出范围,并且您的代码将恢复为使用原始实例(您已关闭它,因此没有输出!)

  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,";
  } 

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,";
  } 
暖树树初阳… 2024-11-09 14:07:28

尝试删除 ofstream fsTestOutput;行从循环内,基本上您正在错误的范围内创建一个 ofstream 。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文