ofstream不会刷新 [英] ofstream doesn't flush

查看:122
本文介绍了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.

这篇关于ofstream不会刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆