How does one determine the environment newline1 in C++? Google yields many results for C# and .NET but I didn’t see any way to do it for non-CLI C++.
Additional info: I need to scan a const char* for the character(s).
1By “environment newline” I mean \r\n on Windows, \n on Linux, and \r on Mac.
std::endlinserts a newline appropriate for the system. You can use aostringstreamto determine the newline sequence as a string at runtime.EDIT: * See comments below on why this probably won’t work.
If you know that your platforms will be limited to Windows, Mac, and Unix, then you can use predefined compiler macros (listed here) to determine the endline sequence at compile-time:
Most non-Windows and non-Apple platforms are some kind of Unix variant that uses
\n, so the above macros should work on many platforms. Alas, I don’t know of any portable way to determine the endline sequence at compile time for all possible platforms.