C ++ ofstream换行符 [英] C++ ofstream line break

查看:525
本文介绍了C ++ ofstream换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

#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中的所有内容都在同一行上.我该如何解决?

The problem is, everything in output.dat is on the same line. How can I resolve this?

谢谢!

我正在使用Windows查看文件,并使用Linux进行编译.这就是为什么我遇到这个问题的原因.在Linux端使用cat output.dat来查看文件内容,可能表明Windows与Linux的换行符在当时有所不同.

I was using Windows to see the files and Linux to compile. This is why I was running into this issue. Using cat output.dat on the Linux side to see the file contents would have revealed that Windows vs. Linux line breaks are different at the time.

推荐答案

std :: endl已经插入换行符,因此文件中有换行符.我假设您是在LF系统(Linux或其他类似UNIX的系统)上生成文件,然后在CRLF系统上查看文件.在这种情况下,您的换行符不会在文本编辑器中显示为换行符. unix2dos是你的朋友.

std::endl already inserts a linebreak, so you have linebreaks in your file. I assume you are generating your file on a LF system (Linux or other UNIX-like) and viewing it on a CRLF system. In this case, your linebreak won't show in the text editor as a linebreak. unix2dos is your friend.

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

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