The EOF flag is only triggered after you attempt to read past the end of file.
Reading upto
the end of file will not trigger it.
This is why most code looks like this:
while(ifs.read(&bar,sizeof(bar)))
{
// Do Stuff
}
If the result of the read() goes upto the EOF the loop will be entered.
If the result of the read() goes past the EOF the loop will NOT be enetered
- NOTE: read() will only go past the EOF if there are zero bytes left in the file. Otherwise it will read upto the end of the file. So The loop is always entered if there was somthing left in the file.
The reason is the result of the read (the return value) is a reference to the stream. If the stream is used in a boolean context (such as the if test expression) it is converted into a type that can be used in such a context. The result of this conversion tests the EOF flag (in addition to a couple of others) and returns the quivalent of false if EOF is true.
Note:
This techniques works better if you overload the operator << for your Bar class as this should read in exactly what it needs for the object without going past the EOF. It is then easier to make your objects read exactly upto the end of the file without going over. The thing I worry about with read is what should happen if read() wants 10 bytesand there are only 5 bytes in the file, what happens with the partially filled object?
If you want to continue using your style the code should look like this:
ifs.open("foo");
while (!ifs.eof())
{
ifs.read(&bar,sizeof(bar));
if (ifs.eof())
{ break;
}
// Do Stuff
}