problem with cross-platform io su txt file

Discussion in 'C++' started by Giff, Jul 9, 2008.

  1. Giff

    Giff Guest

    Hi,

    I hope this post is not too OT.
    I am porting some code from Windows to Linux and I am having a problem
    with a routine that skips comments and empty lines in a txt file.
    The following code has worked nicely on Windows, but it does not on
    Linux, I understand that is because the two OS uses different way of
    ending a line.

    char next = static_cast<char>(is.peek());

    //if the first character of the line is a comment or the endline
    while( next == '\n' || next == comment ) {

    //ignore the whole line
    is.ignore( 10000, '\n' );

    //check the next line
    next = static_cast<char>(is.peek());

    }

    How would you suggest I should modify the above function to work under
    Linux (or better under any OS)?

    Thanks a lot,
    G
     
    Giff, Jul 9, 2008
    #1
    1. Advertising

  2. Giff

    Giff Guest

    Giff wrote:

    > char next = static_cast<char>(is.peek());


    of course, *is* is a istream object.
     
    Giff, Jul 9, 2008
    #2
    1. Advertising

  3. Giff

    Giff Guest

    Giff wrote:
    > Hi,
    >
    > I hope this post is not too OT.
    > I am porting some code from Windows to Linux and I am having a problem
    > with a routine that skips comments and empty lines in a txt file.
    > The following code has worked nicely on Windows, but it does not on
    > Linux, I understand that is because the two OS uses different way of
    > ending a line.


    So, well, very easily, I don't only check for '\n', but also for '\r'.
    Now it works.
     
    Giff, Jul 9, 2008
    #3
  4. Giff

    James Kanze Guest

    On Jul 9, 10:07 pm, Paavo Helde <> wrote:

    [...]
    > In my code, I always open all files in binary mode. I don't
    > want the run- time library to silently change the file content
    > under my feet. In my mind, the "text file mode" is
    > fundamentally flawed as the so-called text file may easily
    > originate from another system with other conventions, so the
    > run-time library level translation is not possible in general
    > (as demonstrated by OP).


    I'm gradually tending in the same direction. Text mode works
    well as long as you're not sharing disks; file transfer programs
    should take care of the newline mappings. You run into the
    problems when sharing disks, however; I often edit under Unix,
    but compile and run under Windows.

    Of course, '\r' is white space, so in practice, if your code
    accepts trailing whitespace in a line (which it should), the
    fact that you "see" an extra \r at the end of the line shouldn't
    cause any problems.

    --
    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, Jul 10, 2008
    #4
  5. Giff

    Giff Guest

    Paavo Helde wrote:

    > In my code, I always open all files in binary mode. I don't want the run-
    > time library to silently change the file content under my feet.


    Mmmm, the problem is that I am not the one writing the text files I am
    trying to read... I could fix the comments/empty lines issue, but other
    parts of the text files also give problems...

    Damn.
     
    Giff, Jul 10, 2008
    #5
    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. mir nazim
    Replies:
    7
    Views:
    559
    Brian Kelley
    Nov 24, 2003
  2. John Benson
    Replies:
    0
    Views:
    309
    John Benson
    Nov 22, 2003
  3. Tim Churches
    Replies:
    2
    Views:
    421
    mir nazim
    Dec 8, 2003
  4. Sameen
    Replies:
    2
    Views:
    459
    Victor Bazarov
    Aug 29, 2005
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,683
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page