portable std::getline and line terminators

F

fft1976

Hi

If I open my ifstream in text mode (default ios_base::eek:penmode?),
std::getline may or may not eat '\r' with '\n', and << '\n' may or
may not write '\r', correct?

(I actually use boost::filesystem::ifstream, but the behavior should
be the same, as it aims to be a replacement)

What I'd like to do is to write the output in the same format the
input was in (even if it's a Unix file on Windows and vice versa).
What's a good way to achieve this? Is binary I/O the only viable
approach?
 
B

Bo Persson

fft1976 said:
Hi

If I open my ifstream in text mode (default ios_base::eek:penmode?),
std::getline may or may not eat '\r' with '\n', and << '\n' may or
may not write '\r', correct?

(I actually use boost::filesystem::ifstream, but the behavior should
be the same, as it aims to be a replacement)

What I'd like to do is to write the output in the same format the
input was in (even if it's a Unix file on Windows and vice versa).
What's a good way to achieve this? Is binary I/O the only viable
approach?

Pretty much, yes.

The text mode I/O is supposed to read and write text files using the
conventions of the operating system you are using. If that is not what
you want...

Note that Unix and Windows are not the only known variants.


Bo Persson
 
J

James Kanze

If I open my ifstream in text mode (default
ios_base::eek:penmode?), std::getline may or may not eat '\r'
with '\n', and << '\n' may or may not write '\r', correct?

Maybe. If you open the ifstream in text mode, you get whatever
the system defines as text mode. Under Unix, there's no
difference between text and binary modes. Under Windows, text
mode uses CRLF (instead of just LF) as a line ending (or
separator, depending on the application), and 0x1A as an end of
file character; otherwise, the two modes are basically similar.
Under some other OS's, text and binary modes may use different,
incompatible file types.

Strictly speaking, std::getline only eats a trailing '\n' (or
whatever character was given it). The modes are handled
considerably upstream of the istream, in filebuf. And end of
line could also be influenced by the imbued locale; in an 8089-1
locale, I would expect 0x85 (next line) to be mapped to '\n',
and in a UTF-8 locale, 0xE2 0x80 0xA8 (line separator).
(I actually use boost::filesystem::ifstream, but the behavior
should be the same, as it aims to be a replacement)
What I'd like to do is to write the output in the same format
the input was in (even if it's a Unix file on Windows and vice
versa). What's a good way to achieve this? Is binary I/O the
only viable approach?

Probably. Ideally, you'd have some sort of locale which could
be used for "foreign" file systems, but I don't know of any
system which actually offers this. And you'd still have the
problem of determining the conventions used on the input file,
and what to do if it is mixed.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top