Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "\n" vs. '\n'

Reply
Thread Tools

"\n" vs. '\n'

 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-08-2007
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:rintf) 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".
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      08-08-2007
On 8 Aug., 12:28, Juha Nieminen <(E-Mail Removed)> wrote:
> 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:rintf) 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

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      08-08-2007
On 2007-08-08 06:28:47 -0400, Juha Nieminen <(E-Mail Removed)> said:

> 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:rintf) 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments