473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"\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 1728
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
5055
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 relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to...
1
8050
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 expected with alphabet and number characters and the ' (single quote &apos; entity) character but does not work if a double quote character " is...
2
20489
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
3625
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 there is a " or ' in the character string. <SCRIPT language=JavaScript> function Add_Term(SearchTerm) {...
5
3443
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 clear. But how should parser react for such situation: I have CORDSYS element with string attribute which consists of value with many &quot;...
3
2691
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 directories contain a file named "cppfile1.h". In my main project I #include "cppfile1.h". I rely on the order of paths in additional include...
5
3424
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; &lt;me@mydomain.com&gt;</Address> </Addresses>
8
3606
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 <br>mais pour avoir des resultats probant il faut pas faire les mariolles, comme le &quot;fondateur&quot; de bvs
1
2830
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. 1)<tm:bom-expression>{Conf.getEquityConfLookupFields().getEventFieldText(&quot;AdditionalDisruption&quot;,&quot;Change in Law&quot;)}</tm:bom-expression> 2)07:41:08 Default ( call ( . ( call ( . Conf getEquityConfLookupFields ) ) ...
4
3873
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 &quot;&quot; instead of "" otherwise I get an error message. Can anyone suggest how to write it so that it writes &quot; instead of "". I have tried all...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.