problem with eof loop

This is a discussion on problem with eof loop within the C++ Programming forums, part of the General Programming Boards category; I'm not sure what's happening here. But this is just a simple loop that reads data into a struct (account) ...

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    problem with eof loop

    I'm not sure what's happening here. But this is just a simple loop that reads data into a struct (account) from a file until the eof is reached and then displays it. The problem is, for some reason, the last entry is displayed twice. How could this be?



    Code:
    void displayAccounts(account *a)
    {
    fio.seekg(ios_base::beg);
    while(!fio.eof())
    {
    fio.read((char *) a, (sizeof account));	
    cout << "Account type: "<< a->type << "\n";
    cout << "Name: "<< a->name << "\n";
    cout << "Address: " << a->address << "\n";	
    cout << "Balance: " << a->balance << "\n";
    cout << "PIN: " << a->pin << "\n\n";
    }
    }


    Thanks in advance, Invincible
    Last edited by Invincible; 05-19-2002 at 03:31 PM.
    "The mind, like a parachute, only functions when open."

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    It's that deja-vue feeling (again)
    http://www.cprogramming.com/cboard/s...threadid=18036

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Thumbs up

    feof() returns true only AFTER a read function has returned EOF
    Thanks Salem.
    "The mind, like a parachute, only functions when open."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 05:37 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 10:40 AM
  3. while loop problem
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 12:21 PM
  4. While Loop Problem
    By Ripper1 in forum C++ Programming
    Replies: 17
    Last Post: 07-24-2003, 03:36 AM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21