The function of the system call read is to read nbytes bytes of data from a file associated with the file descriptor files and place them in the data area buf. It returns the actual read-in character, which may be less than the requested number of bytes. If the read call returns 0, it means that no data has been read and the end of the file has been reached. Similarly, if 1 is returned, it indicates that the read call has an error.
#include <unistd.h>
<span style= "font-family:arial, Helvetica, Sans-serif;" >size_t Read (int fildes, void *buf, size_t nbytes);</span>
The following program test.c copies the first 128 bytes of the standard input to the standard output. If the input is less than 128 bytes, copy them all over.
#include <stdlib.h> #include <unistd.h>int main () { char buffer[128]; int nread; Nread = Read (0,buffer,128); if (nread = =-1) write (2, "A read error has occured\n", +); if (write (1,buffer,nread)! = nread) write (2, "A write error has occured\n", +); Exit (0);}
Test results
Linux underlying file access---read system call