Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the Newline character for the environment a C++ program is being compiled on

Tags:

c++

newline

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.

like image 987
Seth Carnegie Avatar asked Jul 28 '11 19:07

Seth Carnegie


People also ask

What is the newline character in C?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

What is the use of environment newline in C#?

NewLine can be used in conjunction with language-specific newline support such as the escape characters '\r' and '\n' in Microsoft C# and C/C++, or vbCrLf in Microsoft Visual Basic. NewLine is automatically appended to text processed by the Console. WriteLine and StringBuilder. AppendLine methods.

Which of the following words is used for printing newline in C ++?

The endl function, part of C++'s standard function library inserts a newline character into your output sequence, pushing the subsequent text to the next output line. Note that endl must be free of quotation marks; otherwise, the program will treat it as a string.


2 Answers

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.

like image 34
MSN Avatar answered Sep 28 '22 10:09

MSN


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.

like image 110
Emile Cormier Avatar answered Sep 28 '22 08:09

Emile Cormier