C++ ofstream 未按预期运行
我有一种感觉,我错过了一些明显的东西,但似乎无法解决以下问题:
请参阅下面的代码。它所做的只是循环 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建新文件时,您将在
if
语句中声明第二个ofstream fsTestOutput
。第二个
ofstream
的作用域仅限于您的if
语句,因此它可以在您的if
中正常工作。但是,当您离开if
语句并返回到for
循环时,新的 ofstream 实例将超出范围,并且您的代码将恢复为使用原始实例(您已关闭它,因此没有输出!)You're declaring a second
ofstream fsTestOutput
in yourif
statement when you create the new file.That second
ofstream
has scope local to yourif
statement, so it'll work fine within yourif
. However, when you leave theif
statement and head back to thefor
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!)尝试删除 ofstream fsTestOutput;行从循环内,基本上您正在错误的范围内创建一个 ofstream 。
Try removing the ofstream fsTestOutput; line from within the loop, basically you are creating an ofstream in the wrong scope.