1

If I have opened an output stream like this:

    ofstream to(output_file);

How may I print new line (Looking to support different os)?

  1. to << "\n";

  2. to << "" <<endl;

1 Answers1

1

End of line notation, '\n' is used in most of the systems I know except Windows (MS Dos) which use '\r\n'. What is different in between those two is that '\n' in most systems will return cursor to the beggining of new line, where in MS DOS it will go to the same character as previous line, so if you have something like this:

Hello\nWorld!!!

in most systems it will output:

Hello
World!!!

where as in MS DOS it will output:

Hello
     World!!!

So to overcome this issue of different systems treating newline differently we have std::endl, which will place correct notation for the correct system. In my code I might be bad, but I am mostly using '\n', but on the other hand I have not been using Windows as my dev machine that much.

One more point, printing out "" is useless.

  • @CherryDT ok will fix this. –  Aug 04 '20 at 20:55
  • Actually, by ASCII definition, '\n' is linefeed (LF) and '\r' is Carriage Return (CR). Back in the days of the Teletype, the CR would return the carriage to the left margin. If you didn't advance a line using LF, you could overwrite the text. This is how some ASCII art works and how bolding worked. Later on, some *nix lovers decided using LF as a line end would save some characters and that's what they decided on. Windows and other consoles decided to use both CR and LF. The definition of *most systems* depends on marketing and sales. You could have a terminal on windows only using LF. – Thomas Matthews Aug 04 '20 at 21:06
  • @ThomasMatthews good point, I know most of these facts, but I do not remember, if I remember correctly there was something else that was using '\n' to do both before Unix. I might be wrong, thus I did not want to say that Unix is using '\n'. –  Aug 04 '20 at 21:14