Can read(2) return zero when not at EOF?

After some research, there actually are some circumstances under which it will return 0 that you might not think of as being "EOF".

For the gritty details, see the POSIX definition for read(): http://opengroup.org/onlinepubs/007908775/xsh/read.html

Some notable ones are if you ask it to read 0 bytes -- double check that you're not accidentally passing 0 to it -- and reading past the end of the "written" portion of the file (you can actually seek past the end of the file, which "extends" the file with zeroes if you write there, but until you do, "EOF" is still at the end of the already-written portion).

My best guess is that you're getting into a timing problem somewhere. Some questions you need to ask are "How are these files being written?" and "Am I sure they're not zero-length when I try to read them?". For the second one, you could try running a stat() on the file before reading it to see what its current size is.


The only other case that I can think of read() returning 0 is if you pass in nbytes as 0; sometimes that can happen if you're passing in the size of something or other as a parameter. Could that be what's happening right now?

If the file is not ready to be read, what should happen is read returns -1 and errno is set to EAGAIN.

Tags:

Linux

C++

Eof