read(2) - What does return 0 mean?

jkt

New Member
Joined
Mar 12, 2024
Messages
1
Reaction score
0
Credits
16
Manual page read(2) at https://man7.org/linux/man-pages/man2/read.2.html, states:

On success, the number of bytes read is returned (zero indicates end of file)….It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.

How do we differentiate EOF from a read of 0 characters that is simply less than the number of bytes requested?

I have blissfully been using the read syscall for decades and now I have made the mistake of reading the manual. Am I missing something?
 


How do we differentiate EOF from a read of 0 characters that is simply less than the number of bytes requested?
According to docs:
If the file offset is at or past the end of file,
no bytes are read, and read() returns zero.
Therefore return value of 0 means specifying a count of bytes that is beyond file size so nothing is read and thus count of bytes read is 0.

For read operation to be success (positive reutrn value) you need to specify less or exact count of bytes to read.
 

Members online


Top