C ++ Ofstream - new line

I am trying to make a new line in a stream, but it does not work. Everything is written on the same line in OUTPUT.txt

std::ofstream output; output.open("OUTPUT.TXT"); output << "sometext" << "\r\n" << "sometext" << "\r\n" << "sometext"; output.close(); 

I also tried

 output << "sometext" << std::endl << "sometext" << std::endl << "sometext"; 

and

 output << "sometext" << "\n" << "sometext" << "\n" << "sometext"; 

and

 output << "sometext" << '\n' << "sometext" << '\n' << "sometext"; 

Everything was written on one line, no new lines ... Am I missing something?

+2
source share
4 answers

If you use cygwin g ++, he had problems converting "\ n" to Windows-style CRLF with some settings , I tried a simple version of your snippet, and everything turned out fine. Try opening it in another text editor and see if the problem persists.

+2
source

Maybe. You are reading a different copy of the file than the one your program writes. Delete the file, then run your program.

0
source

In such cases, I find it best to look at the file in a hex editor rather than a text editor that may have its own ideas on how to render the text, and this can be useful for finding annoying non-printable characters that might touch you.

You did not say which platform you are working on, but if you are in windows, I would recommend using HxD .

If you are on Windows, you will see something like this:

 Offset(h) 00 04 08 0C 10 14 18 1C 00000000 736F6D65 74657874 0D0A736F 6D657465 78740D0A 736F6D65 74657874 0D0A sometext..sometext..sometext.. 

The sixth sequence 0D0A is the character '\ n' in Windows in the above example.

0
source

use

 output.open("OUTPUT.TXT", ios::out); 

Recall that ios::binary cannot be used.

0
source

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


All Articles