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

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

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

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. Joe
    Replies:
    2
    Views:
    851
    Sharad Kala
    Jan 20, 2004
  2. DJP
    Replies:
    7
    Views:
    8,035
    glen herrmannsfeldt
    Oct 21, 2004
  3. DJP
    Replies:
    16
    Views:
    1,210
    Villy Kruse
    Oct 21, 2004
  4. Replies:
    1
    Views:
    1,597
    Victor Bazarov
    Nov 6, 2007
  5. ramana
    Replies:
    5
    Views:
    500
    ramana
    Dec 26, 2007
  6. ramana
    Replies:
    7
    Views:
    401
    red floyd
    Dec 27, 2007
  7. KB
    Replies:
    2
    Views:
    633
  8. Felix.leg
    Replies:
    2
    Views:
    636
    Christopher
    Feb 7, 2012
Loading...