473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

"\n" vs. '\n'

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
2 1712
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
3
by: NecroJoe | last post by:
I am using PHP to generate a little javascript for one of my pages. In short it allows a user to select a value from a list and pop it into a form field on a seperate page. This works well unless...
5
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of &quot; entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
8
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n ...
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.