views:

495

answers:

5

Hi everyone,

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.

EDIT

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.

+1  A: 

Just open a file in text mode

FILE *fp = fopen( "your_file.txt", "w+t" );

and then

fprintf( fp, "some string and integer %d\n", i );
fclose(fp);

and the OS will take care of the EOL accordingly to its standards.

Simone Margaritelli
How very ... C of you :-)
paxdiablo
Eheh i'm an old school one XD
Simone Margaritelli
Now you've forgotten to close the file. :P
GMan
Please don't fopen() in production code, for reasons of exception safety. Use STL RAII equivalents.
Pavel Radzivilovsky
C'mon it was just an example, do you expect to have a file named "your_file.txt" in a production environment too?
Simone Margaritelli
@Simone: It's easy to see that the file name needs to adapted from your example. It's nigh impossible to see that one shouldn't use the C std lib for dealing with files.
sbi
I'm not seeing how the OS will take care of the EOL. In that example the EOL character will be \n, which the OS should not change.
Nathan Adams
+7  A: 

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.

Steve Jessop
+1  A: 

Well, the STL has std::endl, which you can use as

std::cout << "Hi five!" << std::endl;

Note that besides adding an endline, std::endl also flushes the buffer, which may have undesirable performance consequences.

dimatura
Err... that's not STL, that's iostreams.
Billy ONeal
I agree with BillyONeal. Also, when you just want an `'\n'`, just write `'\n'`, not `std::endl`. I've once seen a speedup by a factor of 8 after replacing needless `std::endl` by `'\n'`.
sbi
Oh, I didn't know those are separate entities. I'd just assumed that they because they share the namespace. Thanks for the correction.
dimatura
+1  A: 

Files, even text files, are often transferred between machines, so "os-specific new line character" is an oxymoron.

It is though true that operating systems have a say on that matter, particularly one operating systems aka Windows, although many windows programs will read \n-spaced files correctly, even though the winapi multiline edit control would not. I suggest you consider twice what is the right for you: it's not necessarily what your OS recommends. If your files are ever to be stored on removable media, do not use OS standard. Use global standard, 0xA.

Pavel Radzivilovsky
+2  A: 

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 write to a file (not a stringstream) very quickly. Or I suppose it might eliminate a virtual function call into the stringstream. Try it if profiling shows time spent inside std::endl.

Potatoswatter
I figured that using '\n's everywhere is totally fine and OS (read Windows) automatically handles it to become \r\n when I write to console and to files. It is more than enough for me at this point.
Andrew