8

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.

Pablo Canseco
  • 554
  • 1
  • 10
  • 24
  • 2
    possible duplicate of [C++ Ofstream a new line](http://stackoverflow.com/questions/6352283/c-ofstream-a-new-line) – karlphillip Mar 14 '12 at 16:30
  • 2
    [Inconceivable](http://www.youtube.com/watch?v=1-b7RmmMJeo). Are you *sure* output.dat has only one line? How are you checking? Also, what operating system and compiler are you using? – Robᵩ Mar 14 '12 at 16:30
  • I am using windows and notepad to check the output file. – Pablo Canseco Mar 14 '12 at 16:32
  • 1
    +1 for compilable example. I just compiled and ran this using VS2010 and newlines appeared correctly (my "input.dat" contained "4\n"). What is your compiler? – hmjd Mar 14 '12 at 16:32
  • Our compile command is g++ -Wall source.cpp -o test I'm assuming i'm using a compiler named g++? – Pablo Canseco Mar 14 '12 at 16:34
  • For reference... where are you running `g++`? And where do you run the program? On a linux system? On a windows system using cygwin? –  Mar 14 '12 at 16:50

2 Answers2

7

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

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • 5
    I'd recommend against this. Except that if you're writing files under Unix, and you want Windows line endings, there's not much alternative. (I wonder if anyone has implemented a `filebuf` which takes an additional formatting option, to specify the line ending convention that should be used for output.) – James Kanze Mar 14 '12 at 16:43
  • @JamesKanze : indeed, I couldn't agree more, but it seems that that is what happens here. Working with `filebuf` is better, and I would love to see it here. –  Mar 14 '12 at 16:50
  • @JamesKanze by the way I doubt if our friend here, can go to that extent, and that he is having such a broad range of applications to have his code working on that he should try `filebuf`. –  Mar 14 '12 at 16:52
  • @g24l I wasn't recommending that the OP write a new `filebuf`:-). I was expressing a sort of a wish that some implementer would extend his `filebuf` in such a fashion. (It would be nice under Linux if I had a `filebuf` option to convert the CRLF into a single `'\n'` as well. But since `'\r'` is white space, and you always want to allow trailing white space, it's less of a problem.) – James Kanze Mar 14 '12 at 17:36
  • @Pablo @JamesKanze Late to the game, but implementing a `filebuf` is overkill. After all, `std::endl` is a stream manipulator. Similarly, a custom ostream manipulator would take far less effort and be far less complicated. – Spencer Jan 19 '17 at 15:47
2

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.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • I am using windows and notepad to check the output file. – Pablo Canseco Mar 14 '12 at 16:32
  • 3
    Even if he generates under Unix and reads under Windows, most editors will display it correctly. If that's his problem, the correct solution is to use a different editor; a true Windows program will output CRLF, but be agnostic for input (just as a true Windows program will output `\` as a path separator, but accept both `\` and `/`). – James Kanze Mar 14 '12 at 16:36
  • 2
    @Pirate43 Notepad is useless. Try anything else (even `type filename` from a console window). – James Kanze Mar 14 '12 at 16:41
  • 1
    If using Cygwin to run gcc under Windows, there is an install option to have either Windows or Unix compatible text files. – Bo Persson Mar 14 '12 at 16:47
  • @James Kanze true, but it's only for a small class assignment and it's easy to use. – Pablo Canseco Mar 14 '12 at 20:16