Q: std::getline and portability

F

Frank Schmitt

Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(input,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.

Thanks in advance & regards
frank
 
S

Sam Holden

Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(input,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.

Assuming the program is running on a system that uses \n as the
line terminator (such as every unix I've heard of) then yes.

When you transfer text files between systems you need to convert them
to the destination systems format. The line terminator is the most
obvious conversion, but occassionaly there are character set issues too.
 
A

Attila Feher

Frank said:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(input,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.

It is conformant AFAIK. A platform only needs to be prepared for its own
file format.
 
L

llewelly

Frank Schmitt said:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(input,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.

Only if (a) you opened the file in text mode and *not* in binary mode,
and (b) your platform defines CR LF as a newline (like windows,
but not like unix or mac).
 
L

llewelly

Frank Schmitt said:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(input,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant?
Yes.

My impression was that getline
should remove '\r\n' as well as '\n'.

Not on unix. On unix \n is just LF, so that is all that is
removed. The CR windows put there is not part of a unix newline,
and is left alone. If you want it removed you must do so
yourself.
 
F

Frank Schmitt

llewelly said:
Not on unix. On unix \n is just LF, so that is all that is
removed. The CR windows put there is not part of a unix newline,
and is left alone. If you want it removed you must do so
yourself.

Ah, I feared so.

Thanks to all for the quick & informative answers
frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top