ofstream不会刷新
问题描述:
我有以下代码,在Suse 10.1/G ++ 4.1.0上运行,并且未写入文件:
I have the following code, running on Suse 10.1 / G++ 4.1.0, and it doesn't write to the file:
#include <fstream>
#include <iostream>
int main(){
std::ofstream file("file.out");
file << "Hello world";
}
已正确创建并打开文件,但该文件为空. 如果我将代码更改为:
The file is correctly created and opened, but is empty. If I change the code to:
#include <fstream>
#include <iostream>
int main(){
std::ofstream file("file.out");
file << "Hello world\n";
}
(在文本中添加\n
),它可以正常工作.
我也尝试刷新ofstream,但是没有用.
(add a \n
to the text), it works.
I also tried flushing the ofstream, but it didn't work.
有什么建议吗?
答
如果使用cat
检查文件,则可能是您的shell配置错误,如果没有行尾,则不会打印该行.std::endl
添加\n
和刷新.
If you check your file doing a cat
, it may be your shell that is wrongly configured and does not print the line if there is no end of line.std::endl
adds a \n
and flush.