std::endl
inserts a newline appropriate for the system. You can use a ostringstream
to determine the newline sequence as a string at runtime.
#include <sstream>
int main()
{
std::ostringstream oss;
oss << std::endl;
std::string thisIsEnvironmentNewline = oss.str();
}
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:
#ifdef _WIN32
#define NEWLINE "\r\n"
#elif defined macintosh // OS 9
#define NEWLINE "\r"
#else
#define NEWLINE "\n" // Mac OS X uses \n
#endif
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.