is there any way to automatically use correct EOL character depending on the OS used?
I was thinking of something like std::eol
?
I know that it is very easy to use preprocessor directives but curious if that is already available.
What I am interested in is that I usually have some messages in my applications that I combine later into a single string and I want to have them separated with a EOL. I know that I could use std::stringstream << endl
but it seems to be an overkill sometimes instead of a regular append.
CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.
On Windows, line-endings are terminated with a combination of a carriage return (ASCII 0x0d or \r) and a newline(\n), also referred to as CR/LF. On the Mac Classic (Mac systems using any system prior to Mac OS X), line-endings are terminated with a single carriage return (\r or CR).
The End of Line (EOL) sequence ( 0x0D 0x0A , \r\n ) is actually two ASCII characters, a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line.
Many older Mac applications use "Mac-style” line endings. This means that each line in a text file ends with a carriage return character (character 13/0xD, often abbreviated CR). Follow this answer to receive notifications.
std::endl
is defined to do nothing besides write '\n'
to the stream and flush it (§27.6.2.7). Flushing is defined to do nothing for a stringstream
, so you're left with a pretty way of saying mystringstream << '\n'
. The standard library implementation on your OS converts \n
appropriately, so that's not your concern.
Thus endl
is already the ultimate in performance and portability, and the only other thing you could want is << '\n'
if you are trying to efficiently write to a file (not a stringstream). Well, << '\n'
does also eliminate the pointless virtual call to stringbuf::flush
. Unless profiling shows that empty function call to be taking time, don't think about it.
If you want to write a line separator to a stream:
std::cout << '\n';
or
std::cout << "\n";
or
std::cout << "whatever you were going to say anyway\n";
If the stream is text mode and the OS uses anything other than LF as a separator, it will be converted.
If you want to write a line separator and flush the stream:
std::cout << std::endl;
If you have binary-mode output for whatever reason, and you want to write a platform-specific line break, then I think you might have to do it indirectly (write '\n'
to a text stream and then examine it in binary mode to see what you get). Possibly there's some way to directly get the line break sequence from the implementation, that I'm not aware of. It's not a great idea, anyway: if you're writing or reading a file in binary mode then it should be in a format which defines line breaks independently of the OS, or which doesn't have lines at all. That's what binary mode is for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With