在关闭文件之前实时写入文件

0 c++ caffe

我使用以下命令将Solver()函数(第三个库中的函数)发出的输出结果写入文件中:caffe

\n\n
if(std::freopen("redir.txt", "w", stdout)) {\n    std::printf("stdout is redirected to a file\\n"); // this is written to redir.txt\n    solver->Solve();\n    std::fclose(stdout);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但由于该Solve()函数连续发出输出,但在执行redir.txt\xe2\x80\x8d 之前不会更新。std::fclose(stdout);所以我无法实时看到结果。

\n\n

如何实时更新我的​​文件?

\n

Jes*_*uhl 5

定期使用std::flush将写入(缓冲)的数据刷新到文件中。

但不要flush太频繁,否则性能会受到影响。

  • 不知道为什么这不是被接受的答案。它指向文档并且包含比已接受的文档更多的信息。 (3认同)