gpt4 book ai didi

C++ ofstream换行符

转载 作者:可可西里 更新时间:2023-11-01 17:46:55 30 4
gpt4 key购买 nike

这是我的代码:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile ("input.dat", ios::in);
ofstream ofile ("output.dat",ios::out);

int num;
ifile >> num;
ofile << num;
ofile << endl;
ofile << "Did we go to new line?";
ofile << endl;

return 0;
}

问题是,output.dat 中的所有内容都在同一行。我该如何解决这个问题?

谢谢!

编辑:我使用 Windows 查看文件并使用 Linux 进行编译。这就是我遇到这个问题的原因。在 Linux 端使用 cat output.dat 查看文件内容会发现当时 Windows 与 Linux 的换行符不同。

最佳答案

std::endl 替换为 "\r\n" 以获得 CRLF 而不仅仅是 LF。

关于C++ ofstream换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9705923/

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