Tutorial :Portable end of line



Question:

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.


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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.


Solution:5

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »