If endl is a function call and/or macro that magically knows the right line ending for whatever ultimately stores or reads the output stream, then, ugly though it is, endl is the right thing to use.
If a language or compiler automatically “do(es) the right thing” with \n as well, then check your local style guide. Is this your code? Do what you will. Is this for your company? Better to check what’s acceptable.
If you want to guarantee a Unix line ending use \012 instead. Or \cJ if your language is sufficiently warped.
Ah don’t worry, if you do fopen(file, "w") on Windows and forget to use "wb" flag, it will automatically replace all your \n with \r\n when you do fwrite, then you will try to debug for half a day your corrupted jpeg file, which totally never happened to me because I’m an experienced C++ developer who can never make such a novice mistake.
If
endl
is a function call and/or macro that magically knows the right line ending for whatever ultimately stores or reads the output stream, then, ugly though it is,endl
is the right thing to use.If a language or compiler automatically “do(es) the right thing” with
\n
as well, then check your local style guide. Is this your code? Do what you will. Is this for your company? Better to check what’s acceptable.If you want to guarantee a Unix line ending use
\012
instead. Or\cJ
if your language is sufficiently warped.It’s a “stream manipulator” function that not only generates a new line, it also flushes the stream.
Ah don’t worry, if you do
fopen(file, "w")
on Windows and forget to use"wb"
flag, it will automatically replace all your\n
with\r\n
when you dofwrite
, then you will try to debug for half a day your corrupted jpeg file, which totally never happened to me because I’m an experienced C++ developer who can never make such a novice mistake.