C++ ofstream line break

Pablo Canseco picture Pablo Canseco · Mar 14, 2012 · Viewed 26.8k times · Source

This is my code:

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

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

Thanks!

EDIT: 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.

Answer

user677656 picture user677656 · Mar 14, 2012

Replace std::endl with "\r\n" to get CRLF instead of just LF.