In the POSIX API, read()
returns 0 to indicate that the end-of-file has been reached. Why isn't there a separate function that tells you that read()
would return zero -- without requiring you to actually call read()
?
Reason for asking: Since you have to call read()
in order to discover that it will fail, this makes file reading algorithms more complicated and maybe slightly less efficient since they have to allocate a target buffer that may not be needed.
What we might like to do...
while ( !eof )
{
allocate buffer
read to buffer
process buffer
}
What we have to do instead...
while ( true )
{
allocate buffer
read to buffer
if ( eof ) release buffer, break;
process buffer
}
Additionally, it seems like this behavior propagates itself into higher-level APIs such as fread()
and feof()
in C -- and creates a lot of confusion about how to use feof()
correctly: