problem with getline

Discussion in 'C++' started by bg, Jun 26, 2004.

  1. bg

    bg Guest

    hi all,

    i need to parse a text file, making several passes over it. I'm doing
    something like this:

    ifstream input(args[2]);
    ......
    string line;
    while(getline(input,string)) // first pass works ok
    {
    ...
    }
    // reset fpos to beginning of input and parse again
    input.seekg(0,ios::beg);
    while(getline(input,string)) // second pass - never enters
    {
    ...
    }

    the problem i'm seeing is that the second while is never entered,
    presumaby because i'm not resetting the file back to the begining
    correclty?

    whats the correct way?
    TIA

    i'm using xcode 1.2/gcc 3.3?

    bg
     
    bg, Jun 26, 2004
    #1
    1. Advertising

  2. bg

    David Harmon Guest

    On 26 Jun 2004 15:09:45 -0700 in comp.lang.c++, (bg)
    wrote,
    >while(getline(input,string)) // first pass works ok
    >{
    > ...
    >}
    >// reset fpos to beginning of input and parse again


    When the getline fails (or any input fails) the stream is set to the
    fail state. It will do nothing for you until you call input.clear().

    This issue is vaguely related to topic "[15.3] How can I get std::cin to
    skip invalid input characters?" in Marshall Cline's C++ FAQ. It is
    always good to check the FAQ before posting. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Jun 26, 2004
    #2
    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. John
    Replies:
    10
    Views:
    10,110
    Buster Copley
    Jul 14, 2003
  2. Poof
    Replies:
    1
    Views:
    412
  3. Joe
    Replies:
    4
    Views:
    7,789
    Chris Theis
    Jan 22, 2004
  4. solartimba

    getline problem

    solartimba, Apr 6, 2004, in forum: C++
    Replies:
    1
    Views:
    457
    Leor Zolman
    Apr 6, 2004
  5. Alex

    getline problem

    Alex, Jun 5, 2004, in forum: C++
    Replies:
    6
    Views:
    539
    Victor Bazarov
    Jun 6, 2004
Loading...

Share This Page