1

I'm trying to get an output into a text file. Always when I add to in endl or "\n" it doesn't start a new line, but puts a square in the file instead.

I've done it in Dev-C++ and also with Qt, but always the same result. Do you know where's the problem?

Thanks for your answers.

Tom83B
  • 2,059
  • 4
  • 18
  • 28
  • Show us some code. I don't believe you until you do. ;-) – Johann Gerell Feb 15 '11 at 16:02
  • 1
    The whole point of endl is (I always assumed) to abstract away the issue of platform-specific line ends, and it seems to do that fine for me. Which compiler are you using? Are you working in cygwin, or another pseudo-Unix environment? How are you displaying the text files? –  Feb 15 '11 at 16:11
  • 1
    seems to be duplicate of http://stackoverflow.com/questions/1651936/changing-stdendl-to-put-out-crlf-instead-of-lf – Foo Bah Feb 15 '11 at 16:53

3 Answers3

1

If you want to display a new line in Windows, use "\r\n" instead "\n".

(read more at http://en.wikipedia.org/wiki/Newline#Representations)

Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
  • Thanks, this works. It actually was problem with the text-editor, but this works fine with a regular notepad as well. – Tom83B Feb 15 '11 at 21:35
  • Maybe it works for you at the moment, but it isn't portable. Using std::endl abstracts away the issue of line ends, allowing you to write code in C++ that uses the correct line endings for whatever platform you compile it for. Why choose to write code that may cause problems for Unix/Linux/etc users very similar to the problems you had in Windows? –  Feb 19 '11 at 02:26
1

If you open the file in text mode [i.e. if you used ofstream, dont add ios::bin], the program should write the correct newline characters. notepad may be trying to read it in the wrong format. Try using wordpad and confirm that the characters are correct.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
0

Which text editor are you using to view the file? Usually when you see a "square" its a decoding error, and the text editor didn't read the output correctly;

If you are on Windows, try opening the same file with Notepad and Wordpad, see if the "square" persists in both options. If one shows the correct output - it means the other editor simply can't decode the text!

Kaa
  • 677
  • 6
  • 17