Read data from an open file descriptor.
Syntax
#include <unistd.h>
ssize_t read (int fd, void *buffer, ssize_t nbytes);
Parameters
-
fd
The file descriptor to read data from.
-
buffer
A pointer to the buffer that receives the data.
-
nbytes
Maximum number of bytes to read.
Return Value
The return value is the number of bytes read if successful. Otherwise, the return value is negative and errno is set:
-
EBADF
Bad file descriptor.
Remarks
The number of bytes read may not be the same as the number of bytes specified in the third argument. Most drivers are implemented with non-blocking IO and will return zero (no bytes read) when there is no data available. Partial reads may also be possible depending on the driver.
For information on how to correctly use read
to read data from a driver,
see the appropriate driver documentation and examples. Files residing on
disk can be expected to read either nbytes
of data, the remaining number
of data in the file, or an error, whatever occurs first.