Can read (2) return zero if not on EOF?

According to the read man page (2), it only returns zero when EOF is reached.

However, this seems to be wrong and can sometimes return zero, perhaps because the file is not yet ready for reading? Should I call select () to make sure it is ready before reading a file from disk?

Please note that nBytes: 1,445,888

Code example:

fd_set readFdSet; timeval timeOutTv; timeOutTv.tv_sec = 0; timeOutTv.tv_usec = 0; // Let see if we'll block on the read. FD_ZERO(&readFdSet); FD_SET(fd, &readFdSet); int selectReturn = ::select(fd + 1, &readFdSet, NULL, NULL, &timeOutTv); if (selectReturn == 0) { // There is still more to read. return false; // But return early. } else if (selectReturn < 0) { clog << "Error: select failure: " << strerror(errno) << endl; abort(); } else { assert(FD_ISSET(fd, &readFdSet)); try { const int bufferSizeAvailable = _bufferSize - _availableIn; if (_availableIn) { assert(_availableIn <= _bufferSize); memmove(_buffer, _buffer + bufferSizeAvailable, _availableIn); } ssize_t got = ::read(fd, _buffer + _availableIn, bufferSizeAvailable); clog << " available: " << bufferSizeAvailable << " availableIn: " << _availableIn << " bufferSize: " << _bufferSize << " got " << got << endl; return got == 0; } catch (Err &err) { err.append("During load from file."); throw; } } 

The output is output (when it does not work without reading data):

 available: 1445888 availableIn: 0 bufferSize: 1445888 got: 0 

This runs on centos4 32 bit as a virtual machine using VMware Server 1.0.10. The read file system is a local virtual machine. The host machine is a 32-bit Windows Server.

Uname -a says:

 Linux q-centos4x32 2.6.9-89.0.25.ELsmp #1 SMP Thu May 6 12:28:03 EDT 2010 i686 i686 i386 GNU/Linux 

I noticed that the link http://opengroup.org/onlinepubs/007908775/xsh/read.html is below:

 The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal... If a read() is interrupted by a signal before it reads any data, it will return -1 with errno set to [EINTR]. If a read() is interrupted by a signal after it has successfully read some data, it will return the number of bytes read. 

So, maybe I get a signal that interrupts reading, and therefore the return value is zero due to an error, or is it considered that zero bytes were read?

+6
c ++ linux eof
source share
3 answers

I thought! I had a Uninitialized Memory Read (UMR) and incorrectly searched for the end of the file.

+1
source share

After some research, there really are some circumstances under which it will return 0 that you may not consider "EOF".

See the POSIX definition for read () for details: http://opengroup.org/onlinepubs/007908775/xsh/read.html

Some noteworthy, if you ask him to read 0 bytes - double check that you do not accidentally miss 0 to it - and read the end of the end of the โ€œrecordedโ€ part of the file (you can really find past the end of the file that โ€œextendsโ€ the file with zeros if you you write there, but until you do that, "EOF" is still at the end of the part already written).

My best guess is that you ever run into a synchronization problem. Some of the questions you need to ask are, "How are these files written?" and "I'm sure they don't have zero length when I try to read them?" For the second, you can try running stat () in the file before reading it to find out what its current size is.

+3
source share

The only other case that I can think of read () returning 0 is that you pass nbytes as 0; sometimes this can happen if you pass the size of one or the other as a parameter. Could it be now?

If the file is not ready for reading, then read -1 should happen, and errno should read EAGAIN.

+2
source share

All Articles