gpt4 book ai didi

c++ - ofstream 不刷新

转载 作者:可可西里 更新时间:2023-11-01 16:29:27 26 4
gpt4 key购买 nike

我有以下代码,在 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,但没有用。

有什么建议吗?

最佳答案

如果您检查您的文件执行 cat ,则可能是您的 shell 配置错误,如果没有行尾则不会打印该行。
std::endl 添加了一个 \n and flush。

关于c++ - ofstream 不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3113229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com