the reading of the last line:
1. check if eof (ok)
2. read a line (the last one). fread only reads the bytes that are there and therefore doesn't reach eof.
3. you do something with buf.
4. check if eof (still ok)
5. read a line (which doesn't exist, so you reach eof. no bytes are read, so buf remains the same)
6. you do something with buf (which is teh same)
7. check if eof (not ok)
You want to be careful with the fread() and feof() response. Just like in C++ streams, the read operation may hit the end of file but still return valid data.
A better option is to work off of the return value for fread(), and check afterwards to make sure you hit EOF and not some other error:
1 2 3 4 5 6 7 8
while (fread(..., f) != 0)
{
...
}
if (!feof(f))
{
fooey();
}
In C++ it would be the same:
1 2 3 4 5 6 7 8
while (f.read(...))
{
...
}
if (!f.eof())
{
fooey();
}