portable std::getline and line terminators

Discussion in 'C++' started by fft1976, Jun 8, 2009.

  1. fft1976

    fft1976 Guest

    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?
    fft1976, Jun 8, 2009
    #1
    1. Advertising

  2. fft1976

    Bo Persson Guest

    fft1976 wrote:
    > 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
    Bo Persson, Jun 8, 2009
    #2
    1. Advertising

  3. fft1976

    James Kanze Guest

    On Jun 8, 7:42 am, fft1976 <> wrote:

    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 8, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Bendersky
    Replies:
    1
    Views:
    1,143
    Mike Treseler
    Mar 1, 2006
  2. Frank Schmitt

    Q: std::getline and portability

    Frank Schmitt, Sep 1, 2003, in forum: C++
    Replies:
    5
    Views:
    582
    Frank Schmitt
    Sep 2, 2003
  3. Assertor
    Replies:
    2
    Views:
    2,559
    Assertor
    Feb 26, 2006
  4. Bit Byter
    Replies:
    2
    Views:
    697
    Tom Widmer
    Apr 13, 2006
  5. kj
    Replies:
    19
    Views:
    464
Loading...

Share This Page