Last record added twice when reading ifstream

Discussion in 'C++' started by Kenneth, Mar 23, 2007.

  1. Kenneth

    Kenneth Guest

    Ok, so logically this code seems to make sense but for some reason it
    reads the last record twice before hitting eof. Is ifstream.eof()
    implementation dependent?

    void LoadFile(ifstream &File, List &List)
    {
    User lUser;
    while(!File.eof())
    {
    File >> lUser;
    List.insert(lUser,List.nodeCount());
    }
    }
    Kenneth, Mar 23, 2007
    #1
    1. Advertising

  2. Kenneth

    Stefan Naewe Guest

    On 3/23/2007 1:42 PM, Kenneth wrote:
    > Ok, so logically this code seems to make sense but for some reason it
    > reads the last record twice before hitting eof. Is ifstream.eof()
    > implementation dependent?
    >
    > void LoadFile(ifstream &File, List &List)
    > {
    > User lUser;
    > while(!File.eof())
    > {
    > File >> lUser;
    > List.insert(lUser,List.nodeCount());
    > }
    > }
    >


    Look Here:

    http://www.gnomesane.net/code/doc/noteof/

    S.
    --
    Stefan Naewe
    stefan_DOT_naewe_AT_atlas_DOT_de
    Stefan Naewe, Mar 23, 2007
    #2
    1. Advertising

  3. Kenneth

    Rolf Magnus Guest

    Kenneth wrote:

    > Ok, so logically this code seems to make sense but for some reason it
    > reads the last record twice before hitting eof. Is ifstream.eof()
    > implementation dependent?
    >
    > void LoadFile(ifstream &File, List &List)
    > {
    > User lUser;
    > while(!File.eof())
    > {
    > File >> lUser;
    > List.insert(lUser,List.nodeCount());
    > }
    > }


    This is an FAQ (I'd even say one of the most F AQs).
    It's answered in the C++ FAQ lite at http://www.parashift.com/c -faq-lite .
    Rolf Magnus, Mar 23, 2007
    #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. DJP
    Replies:
    7
    Views:
    7,339
    glen herrmannsfeldt
    Oct 21, 2004
  2. DJP
    Replies:
    16
    Views:
    956
    Villy Kruse
    Oct 21, 2004
  3. ramana
    Replies:
    5
    Views:
    342
    ramana
    Dec 26, 2007
  4. ramana
    Replies:
    7
    Views:
    314
    red floyd
    Dec 27, 2007
  5. Felix.leg
    Replies:
    2
    Views:
    477
    Christopher
    Feb 7, 2012
Loading...

Share This Page