问题
I'm reading data from a file to memory that is opened with:
FILE *f = fopen(path, "rb");
Before I start copying bytes from the file I seek to a start position using:
/**
* Goes to the given position of the given file.
*
* - Returns 0 on success
* - Returns -1 on EOF
* - Returns -2 if an error occured, see errno for error code
* - Returns -3 if none of the above applies. This should never happen!
*/
static int8_t goto_pos(FILE *f, uint64_t pos)
{
int err = fseek(f, pos, SEEK_SET);
if (err != 0) {
if (feof(f) != 0) return -1;
if (ferror(f) != 0) return -2;
return -3;
}
return 0;
}
The problem is that even though I seek to a position way beyond EOF
, this function never returns -1.
According to the reference feof
should return a non zero value when EOF
is encountered.
Why is this? Is the feof
function useless?
Note that I'm currently using the return value of fgetc
to check for EOF
.
回答1:
Seeking simply does not test for the file's end.
The reason for this is that you perhaps might want to do an fwrite()
where you sought to. fseek()
can not know what your plans are for after it was called.
Do an fread()
after seeking behind the file's end and you will have feof()
returning a non-zero value.
回答2:
feof()
is set after an attempt to read fails, so fgets()
or fread()
before.
回答3:
Seeking beyond the EOF will enlarge the file after a subsequent write, so this is conformant, contrary to what others believe. See here: fseek
The fseek() function shall allow the file-position indicator to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.
That's why fseek cannot return EOF. You will later get an EOF if you try to read from a position when nothing had been previously written to that position or behind. So this is all correct behavior.
回答4:
Seeking beyond the end of the file on some operating systems increases the size of the file. As long as there's space to make the file larger, it will never return an error.
来源:https://stackoverflow.com/questions/17263830/fseek-on-a-position-beyond-eof-does-not-trigger-eof-using-feof-how-come