tags:

views:

258

answers:

1

In VS 2005, I have some code that looks like this:

ifs.open("foo");
while (!ifs.eof())
{
    ifs.read(&bar,sizeof(bar));
    loc = ifs.tellg();
    loc += bar.dwHeaderSize;
    // four byte boundary padding
    if ((loc % 4) != 0)
        loc += 4 - (loc % 4);
    ifs.seekg(loc,ios::beg);
}
ifs.close();

The code worked fine in VS 2005, but it fails in VS 2008 Express. From what I can tell, VS 2008 is not returning eof() after the code seeks to the end of the file. Am I missing something? I fixed it by adding an explicit check to see if the seek location exceeded the file size, but I want to be sure I understand ifstream correctly.

+2  A: 

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
}
Martin York