使用流时崩溃



我对以下代码有问题。我想在一个循环中将数据写入不同的文件。但当我在循环中实例化流时,我的程序会无声地崩溃。我已经把代码简化了,所以它不会做任何有用的事情。它只是展示了我无法解释的行为:

#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char* argv[])
{
ofstream test_a("a.json");
cout << "test a" << endl;

ofstream test_b("b.json");
cout << "test b" << endl;

for (int idx = 0; idx < 3; idx++)     
{
cout << "test " << idx << endl;
ofstream test("test_" + to_string(idx) + ".json");
}
return 0;
}

这是输出:

test a
test b
test 0

没有其他,没有错误,没有。

前两个流显然很好,但循环在cout之后的某个地方停止了。我正在使用Windows10和mingw:

gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)

感谢那些有用的评论,我发现我链接了一个std库,它一定来自我的编译器的旧版本。我解决了这个问题,现在它起作用了。

最新更新