Portable way to determine the platform's line separator

Posted by Adrian McCarthy on Stack Overflow See other posts from Stack Overflow or by Adrian McCarthy
Published on 2010-05-01T16:13:38Z Indexed on 2010/05/01 16:17 UTC
Read the original article Hit count: 236

Filed under:
|

Different platforms use different line separator schemes (LF, CR-LF, CR, NEL, Unicode LINE SEPARATOR, etc.). C++ (and C) make a lot of this transparent to most programs, by converting '\n' to and from the target platform's native new line encoding. But if your program needs to determine the actual byte sequence used, how could you do it portably?

The best method I've come up with is:

  1. Write a temporary file in text mode with just '\n' in it, letting the run-time do the translation.
  2. Read back the temporary file in binary mode to see the actual bytes.

That feels kludgy. Is there a way to do it without temporary files? I tried stringstreams instead, but the run-time doesn't actually translate '\n' in that context (which makes sense). Does the run-time expose this information in some other way?

© Stack Overflow or respective owner

Related posts about c++

Related posts about newline