C ++ line break ofstream

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 that everything in output.dat is on the same line. How can i solve this?

Thanks!

EDIT: I used Windows to view files and Linux to compile. That is why I came across this problem. Using cat output.dat on the Linux side to view the contents of the file would indicate that line breaks in Windows and Linux were different at the time.

+6
source share
2 answers

std :: endl already inserts a line, so you have lines in your file. I assume that you create your file on the LF system (Linux or another UNIX-like) and view it on the CRLF system. In this case, your linebreak will not be displayed as a line in a text editor. unix2dos is your friend.

+2
source

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

+5
source

Source: https://habr.com/ru/post/910726/


All Articles