By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

"\n" vs. '\n'

P: n/a
I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".

What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?

Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?

The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)
If you repeatedly call std::getline(is, str, '\n') in a system where
newlines actually consist of more than one character, aren't the
additional characters read into the string (even though they really
shouldn't) instead of skipped? There doesn't seem to be a version of
std::getline() taking a string as delimiter parameter, so you can't give
it a "\n".
Aug 8 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 8 Aug., 12:28, Juha Nieminen <nos...@thanks.invalidwrote:
I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".

What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?

Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?

The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)
If you repeatedly call std::getline(is, str, '\n') in a system where
newlines actually consist of more than one character, aren't the
additional characters read into the string (even though they really
shouldn't) instead of skipped? There doesn't seem to be a version of
std::getline() taking a string as delimiter parameter, so you can't give
it a "\n".
For the user endline is \n and always one character. An operating
systems may encode endline as one character, two characters or no
character at all (I do not know of any other way to do it, but there
could possibly be more). It is the job of the io machinery to
translate these representations into a single endline character unless
you choose to open the stream/file in binary mode.

/Peter

Aug 8 '07 #2

P: n/a
On 2007-08-08 06:28:47 -0400, Juha Nieminen <no****@thanks.invalidsaid:
I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".
'\n' is a character. "\n" is a string consisting of two characters:
'\n' and '\0'.
>
What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?
'\n' is a newline character. When it's written to a text stream the
runtime library will do whatever is appropriate for the system the
program was compiled for. These both have the same effect:

printf("\n");
printf("%c", '\n');
>
Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?
If you're reading in text mode, the system's end-of-line representation
will be translated to the character '\n'. If you're reading in binary
mode you get whatever bytes are in the file.
>
The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)
If you're calling getline() you'd better be using a text stream. The
system's end-of-line representation will be translated to the character
'\n'.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.