how to avoid ifstream::get() when EOF is coming

Discussion in 'C++' started by drmario, Apr 7, 2008.

  1. drmario

    drmario Guest

    I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
    opened a file with ifstream::eek:pen(). The text in the file will have the
    general format of:

    michael jordan 90
    roger moore 79
    your name 100

    So basically a first name, last name, grade, \n. I'm using
    ifstream::get() to one by one read each char of the first and last name into
    a cstring (no not string data type. An array of chars. I suppose
    technically an array of pointers to chars). Then I use ifstream >> to read
    the grade. It's kinda a long story why, but this is the (inefficient, I
    know) way I've chosen to do it and I'm not going to change it.
    Taking James Kanze's previous advice I inserted a buncha statements to
    check the stream's error state flags after every input. I discovered that
    after I read the last student's grade using ifstream >>, the program sets my
    stream's eofbit to true. This of course makes sense. What does NOT make
    sense to me is this after every iteration of reading a line, and echoing it
    to the screen, my program reaches the getch() statement, and correctly
    pauses until I hit a key then continues on. However, on its last iteration,
    after it's read the very last thing in the text file and the eofbit is set
    to true, I have to push like three or four keys.
    Why is that happening? It makes 100% no sense to me. Why does the fact
    that my ifstream's eofbit is set to true cause getch() to misbehave?
    getch() has one job... wait until the next key is hit and return that value.
    Why is this stupid ass ifstream::eofbit making it jack up? From my
    understanding getch() doesn't even acknowledg the existence of a friggin
    file stream, only the standard input stream.
    Yes, as I'm sure you can tell, it pisses me off when Microsoft decides
    not to include every detail of the inner workings of their STL
    functionality. You'd think if there was anyone who could appreciate the
    occasional need for subtle detials it would be the guys writing compilers.
    Anyone I'm done venting. I hope someone can shed some light on why
    getch() suddenly misbehaves after the eofbit is set to true. I'm stuck
    fast, and have been so for weeks.

    cheers,
    Mario
    drmario, Apr 7, 2008
    #1
    1. Advertising

  2. drmario

    Paul Carter Guest

    On Apr 7, 8:54 am, "drmario" <> wrote:
    > I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
    > opened a file with ifstream::eek:pen(). The text in the file will have the
    > general format of:
    >
    > michael jordan 90
    > roger moore 79
    > your name 100
    >
    > So basically a first name, last name, grade, \n. I'm using
    > ifstream::get() to one by one read each char of the first and last name into
    > a cstring (no not string data type. An array of chars. I suppose
    > technically an array of pointers to chars). Then I use ifstream >> to read
    > the grade. It's kinda a long story why, but this is the (inefficient, I
    > know) way I've chosen to do it and I'm not going to change it.
    > Taking James Kanze's previous advice I inserted a buncha statements to
    > check the stream's error state flags after every input. I discovered that
    > after I read the last student's grade using ifstream >>, the program sets my
    > stream's eofbit to true. This of course makes sense. What does NOT make
    > sense to me is this after every iteration of reading a line, and echoing it
    > to the screen, my program reaches the getch() statement, and correctly
    > pauses until I hit a key then continues on. However, on its last iteration,
    > after it's read the very last thing in the text file and the eofbit is set
    > to true, I have to push like three or four keys.
    > Why is that happening? It makes 100% no sense to me. Why does the fact
    > that my ifstream's eofbit is set to true cause getch() to misbehave?
    > getch() has one job... wait until the next key is hit and return that value.
    > Why is this stupid ass ifstream::eofbit making it jack up? From my
    > understanding getch() doesn't even acknowledg the existence of a friggin
    > file stream, only the standard input stream.
    > Yes, as I'm sure you can tell, it pisses me off when Microsoft decides
    > not to include every detail of the inner workings of their STL
    > functionality. You'd think if there was anyone who could appreciate the
    > occasional need for subtle detials it would be the guys writing compilers.
    > Anyone I'm done venting. I hope someone can shed some light on why
    > getch() suddenly misbehaves after the eofbit is set to true. I'm stuck
    > fast, and have been so for weeks.
    >
    > cheers,
    > Mario


    Why don't you post your code? It almost has to be something you are
    doing wrong, but it's hard to tell from just your description of the
    program and not the program itself.

    I think it's more likely that your loop is continuing after the eofbit
    is set to true than getch() is breaking, but it's impossible to check
    without seeing your code.

    Thanks,

    Paul
    Paul Carter, Apr 7, 2008
    #2
    1. Advertising

  3. drmario

    Jerry Coffin Guest

    In article <ZPoKj.2704$>, says...
    > I'm using VC++ 8.0 Express Edition on Windows XP Home Edition. I've
    > opened a file with ifstream::eek:pen(). The text in the file will have the
    > general format of:
    >
    > michael jordan 90
    > roger moore 79
    > your name 100
    >
    > So basically a first name, last name, grade, \n. I'm using
    > ifstream::get() to one by one read each char of the first and last name into
    > a cstring (no not string data type. An array of chars. I suppose
    > technically an array of pointers to chars). Then I use ifstream >> to read
    > the grade. It's kinda a long story why, but this is the (inefficient, I
    > know) way I've chosen to do it and I'm not going to change it.


    So your question is: "How do I do this correctly while insisting on
    doing it completely incorrectly?"

    If you're not using a string, you probably want to use an array of char,
    but almost certainly NOT an array of pointers to char. You're starting
    with a poor idea, but that would be far worse still.

    > Taking James Kanze's previous advice I inserted a buncha statements to
    > check the stream's error state flags after every input. I discovered that
    > after I read the last student's grade using ifstream >>, the program sets my
    > stream's eofbit to true. This of course makes sense.


    No, it does NOT! After reading the last data in the file, eofbit should
    NOT be set yet. You should see eofbit set ONLY when you try to read
    something and the final data in the file had _previously_ been read.

    If you insist on reading the names into arrays of char (why?) you could
    take a look at ifstream::getline to do the job. Since you're reading
    structured data, I'd advise reading the data into a structure:

    struct person {
    const int len = 20;
    char first_name[len];
    char last_name[len];
    int grade;
    };

    std::ifstream &operator>>(std::ifstream &in, person &p) {
    in.getline(first_name, len, ' ');
    in.getline(last_name, len, ' ');
    in >> grade;
    return in;
    }

    Then reading a file full of data in that format into a vector (for
    example) looks something like:

    std::vector<person> people;

    std::copy(std::istream_iterator<person>(input),
    std::istream_iterator<person>(),
    std::back_inserter(people));

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Apr 7, 2008
    #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. Steve Holden

    PyCon is Coming! PyCon is Coming!

    Steve Holden, Jan 5, 2006, in forum: Python
    Replies:
    0
    Views:
    309
    Steve Holden
    Jan 5, 2006
  2. Kobu
    Replies:
    10
    Views:
    626
    Keith Thompson
    Mar 4, 2005
  3. Robbie Hatley
    Replies:
    1
    Views:
    1,822
    P.J. Plauger
    Jul 16, 2006
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    694
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,059
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page