ofstream 不刷新

发布于 2024-09-07 02:37:45 字数 490 浏览 4 评论 0原文

我有以下代码,在 Suse 10.1 / G++ 4.1.0 上运行,但它不会写入文件:

#include <fstream>
#include <iostream>

int main(){
    std::ofstream file("file.out");
    file << "Hello world";
}

文件已正确创建并打开,但为空。 如果我将代码更改为:(

#include <fstream>
#include <iostream>

int main(){
    std::ofstream file("file.out");
    file << "Hello world\n";
}

在文本中添加 \n),它就可以工作。 我也尝试过刷新ofstream,但没有成功。

有什么建议吗?

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";
}

(add a \n to the text), it works.
I also tried flushing the ofstream, but it didn't work.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一紙繁鸢 2024-09-14 02:37:45

如果您使用 cat 检查文件,则可能是您的 shell 配置错误,并且如果没有行尾,则不会打印该行。
std::endl 添加了 \n and 刷新。

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.

与酒说心事 2024-09-14 02:37:45

不知道这是否是您尝试过的,但您应该这样做:

file << "Hello World" << std::flush;

更新由于有用的评论,我将这个答案留在这里

根据反馈,我将修改我的建议:您不必显式调用 std::flush (或 file.close() 就此事而言),因为析构函数会为您完成此操作。

此外,显式调用flush会强制执行I/O操作,这可能不是最优化的方式。遵循底层 iostreams 和操作系统会更好。

显然,OP 的问题与调用或不调用 std::flush 无关,可能是由于在调用文件流析构函数之前尝试读取文件所致。

Don't know if this is what you tried but you should do:

file << "Hello World" << std::flush;

Update; I'm leaving this answer here because of the useful comments

Based on feedback, I'll modify my advice: you shouldn't have to explicitly call std::flush (or file.close() for that matter), because the destructor does it for you.

Additionally, calling flush explicitly forces an I/O operation that may not be the most optimized way. Deferring to the underlying iostreams and operating system would be better.

Obviously the OP's issue was not related to calling or not calling std::flush, and was probably due to attempting to read the file before the file stream destructor was called.

泪痕残 2024-09-14 02:37:45

析构函数应该刷新并关闭文件。

我很确定,错误是在另一个地方,或者

1)您没有在正确的时间点进行检查。在退出“之后”,您在哪一点比较文件的内容,或者在程序退出之前设置断点,然后检查文件内容?

2)程序在退出之前不知何故崩溃了?

The destructor should flush and close the file.

I am pretty sure, the error is an another place, either

1) You do not check at the right point in time. At which point do you compare the content of the file, "after" the exits, or do you set a breakpoint before the program exits and then you check the files content?

2) Somehow the program crashes before it exits?

温柔戏命师 2024-09-14 02:37:45

有效吗

file << "Hello world" << std::endl;

endl 插入换行符并刷新缓冲区。当你说你已经尝试过冲洗它时,你指的是这个吗?

Does

file << "Hello world" << std::endl;

work?

endl inserts a newline and flushes the buffer. Is that what you were referring to when you said that you'd already tried flushing it?

陌路黄昏 2024-09-14 02:37:45

您正在使用 Linux,这是一个兼容 POSIX 的系统。 POSIX 标准定义了行是:

由零个或多个非换行符加上一个
终止换行符。

因此,如果没有换行符,文件将包含 0 行,因此为空。

You are working on Linux, which is a POSIX-compliant system. The POSIX standard defines what a line is:

A sequence of zero or more non-newline characters plus a
terminating newline character.

So without the newline character, the file contains 0 lines and is therefore empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文