Cross-platform newline confusion
What happens is that you write the Unix line endings ('\n'), then transfer it to a Windows machine getting a bitwise identical file, then trying to open the file with a viewer that does not understand Unix line endings (Notepad likely).
From my experience on writing portable code:
- Standardize on ONE line-ending (
'\n'
, LF) on ALL platforms. - Always open your files in binary, even if you write text.
- Let the user who opens the file use a text viewer that understands any line-endings. There are plenty for windows (including Visual Studio, Notepad++, Wordpad and your favorite browser).
Yes, I do think that there is more benefit to everybody to standardize on one thing rather than supporting all of them everywhere. Also I deny the existence of "proper line endings on the proper platform". The fact that Microsoft decided that their native API does not speak UTF-8 or does not understand Unix line endings does not prevent everybody's code from doing that, on Windows. Just make sure not to pass this stuff to WinAPI. Many times you do text processing on your internal data that the system will not ever see, so why the hell do you need to complicate your life by meeting the expectations of those system's internals?
endl
does "work just fine for Linux". Streaming endl
streams a \n
character and flushes the stream. Always.
However, a file stream in text mode will convert this \n
to \r\n
at the implementation layer on Windows, and you'll often find line endings being converted as you transfer the file between platforms, too.
This is probably not a C++ problem, and nothing is "broken"; you should probably configure FileZilla to treat your file as text rather than "binary" (a mode in which line endings are not converted). If your file has no name extension like ".txt" then it probably doesn't do this by default.