9

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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 3
    Why is outputting the "local" newline even necessary? I wish people would stop polluting the world with the impression that "\r\n" is a newline on windows. Just about every single text editor on windows handles "\n" just find. The only counter-example I've ever heard of is Notepad! – André Caron Jul 28 '11 at 20:37
  • This question made me spawn another: http://stackoverflow.com/questions/6865398/inserting-endline-into-a-stringstream – Emile Cormier Jul 28 '11 at 20:48
  • @Andre who said anything about outputting? – Seth Carnegie Jul 28 '11 at 20:48
  • @André: I've always wondered why Notepad thinks it's a typewriter. In this day and age, there is just no use for the carriage return character. – Emile Cormier Jul 28 '11 at 20:54

3 Answers3

6

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.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • Is this the only way to do it? I'd prefer to do it at compile time. – Seth Carnegie Jul 28 '11 at 19:56
  • @Seth: why do you need to know the type of line break at compile-time? – Remy Lebeau Jul 28 '11 at 20:03
  • 1
    @Remy because it requires no computation at run time :) I'll probably have to settle for this though. – Seth Carnegie Jul 28 '11 at 20:07
  • @Seth: You'd only need to compute the line break sequence once. That computation would take probably less than a microsecond. – Emile Cormier Jul 28 '11 at 20:10
  • @Emile, I'm pretty sure that will always return '\n'. The conversion of '\n' to binary is handled by the underlying FILE stream. String streams should not be converting any characters outside of the bounded locale. – MSN Jul 28 '11 at 20:22
  • -1 for encouraging `std::endl`. Your first sentence should read "`'\n'` inserts a newline appropriate for the system." Regardless of how the newline sequence is recorded on disk, if you send `'\n'` to a stream, it will insert the system-appropriate end-of-line sequence. `std::cout << std::endl` is nothing more or less than `std::cout << '\n' << std::flush`. – Robᵩ Jul 28 '11 at 20:23
  • @MSN: I didn't know the conversion was limited to file streams. I don't have a way to test my code on a non-Unix platform at the moment. – Emile Cormier Jul 28 '11 at 20:25
  • I guess I also forgot to mention that I am opening it in `ios::binary` mode because it could be either. – Seth Carnegie Jul 28 '11 at 20:25
  • @Rob: Looks like I had misconceptions about `std::endl`. – Emile Cormier Jul 28 '11 at 20:26
4

For formatted text IO in C++ (and C), the new line character is always '\n'. If you want to know the binary representation of a new line for a given platform and file mode, open a file in the desired mode (e.g., text or binary, UTF-8, UTF-16, MCBS, etc.), write out '\n', close it, reopen it in binary, read in the entire file, and figure out what the actual binary encoding of '\n'. You may also have to account for the end-of-file character as well.

MSN
  • 53,214
  • 7
  • 75
  • 105
  • This works for systems with stream-oriented files (like Unix, DOS, and Windows), but not for record-oriented files (like on VMS). For some systems, the end-of-line indication is not stored as a character sequence at all. – Robᵩ Jul 28 '11 at 20:27
  • @Rob, sure, but who uses VMS anymore? :) (j/k) – MSN Jul 28 '11 at 20:29
  • More seriously, does the _question_ even make sense in VMS? – MSalters Jul 29 '11 at 08:22
1

Generally with a #define. But, for simple applications, opening a file in "text mode" will give you what you need.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38