eof() problems - doubling last value

Here is me code:
1
2
3
4
5
6
// Read from file then display Loan
	while (!loanf.eof())
	{
		loanf.read(reinterpret_cast<char*> (&newLoan), sizeof(Loan));
		displayLoan(newLoan);
	}


For some reason the last Loan object is being read twice. Here is the output I get..you will notice the bottom is doubled when it should not be as I only have 5 loan objects in the file.
5.3 10 20000
3 5 10000
10.9 7 15000
21.3 5 10000
2.3 3 5000
2.3 3 5000
Press any key to continue . . .
Last edited on
Move the eof check after reading
eg:
1
2
3
4
5
6
7
while ( true )
{
    // read
    if ( !loanf ) // will check for bad and fail as well as eof
         break; // exit from the while
    // do your stuff
}
Thanks Bazzy, worked like a charm!
Topic archived. No new replies allowed.