For pset4's recover.c
code, I noticed the following: when I set my while loop to be
while (!feof(inptr))
,
all the images get recovered, including the last, but check50
throws an error (TODO
) when evaluation the last image (015.jpg
).
However, when I rewrote my code, per the walk-through video, to use
while (fread(&buffer, sizeof(buffer), 1, inptr) == 1)
,
again, all the images get recovered, and the check50
passes. I'm curious why that happens. If I think about the last read of data from the 16th JPG, presumably fread
loads that data into the buffer, and then the file's position pointer gets moved to the EOF
spot, in which case feof(inptr)
will return TRUE
. Likewise, if fread
is put in the while
loop and I've already finished reading in all the data, the file position pointer will be at EOF
, and the fread
command will not be able to find 512 more bytes, and so it will return something not equal to 1, and again, I'll break out of the loop.
I'm probably missing something subtle, but any answers would be welcome!