Ssize_t Read(Int Fd, Void *buf, Size_t Count);
read()
Read bytes from a file
Synopsis:
#include <unistd.h> ssize_t read( int fildes, void* buffer, size_t len );
Arguments:
- fildes
- The descriptor of the file that you desire to read from.
- buffer
- A pointer to a buffer where the part tin can shop the information that it reads.
- len
- The number of bytes that you want to read. If len is zero, read() returns zero and has no other issue. If len is greater than INT_MAX (see <limits.h>), the function returns -i and sets errno to EOVERFLOW.
Library:
libc
Use the -l c option to qcc to link confronting this library. This library is usually included automatically.
Description:
The read() office attempts to read len bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buffer.
On a regular file or other file capable of seeking, read() starts at a position in the file given by the file get-go associated with fildes. Earlier successfully returning from read(), the file kickoff is incremented past the number of bytes actually read.
| The read() call ignores advisory locks that you may have set with fcntl(). |
On a file non capable of seeking, read() starts at the current position.
When read() returns successfully, its render value is the number of bytes actually read and placed in the buffer. This number will never be greater than len, although information technology may be less than len for i of the post-obit reasons:
- The number of bytes left in the file is less than len.
- The read() request was interrupted by a signal.
- The file is a piping (or FIFO) or a special file, and has fewer than len bytes immediately available for reading. For example, reading from a file associated with a terminal may render one typed line of data.
If read() is interrupted by a point before it reads any data, it returns -ane and sets errno to EINTR. However, if read() is interrupted past a signal after it has successfully read some information, it returns the number of bytes read.
No data is transferred past the current finish of file. If the starting position is at or afterward the terminate of file, read() returns zilch. If the file is a device special file, the effect of subsequent calls to read() will work, based on the and then-current state of the device (that is, the end of file is transitory).
When attempting to read from an empty pipe or FIFO:
- If no process has the pipe open for writing, read() returns 0 to indicate the end of file.
- If a process has the pipe open for writing, and O_NONBLOCK is set, read() returns -ane, and errno is set to EAGAIN.
- If a process has the pipe open for writing, and O_NONBLOCK is articulate, read() blocks until some data is written, or the pipe is airtight by all processes that had opened information technology for writing.
When attempting to read from a file (other than a pipage or FIFO) that supports nonblocking reads and has no data currently available:
- If O_NONBLOCK is set, read() returns -1, and errno is set to EAGAIN.
- If O_NONBLOCK is articulate, read() blocks until some data is bachelor.
- The O_NONBLOCK flag has no effect if some data is available.
If you call read() on a portion of a file, prior to the stop of file, that hasn't been written, it stores bytes with the value goose egg in buffer.
If read() succeeds, the st_atime field of the file is marked for update.
Returns:
The number of bytes actually read, or -1 (errno is set).
Errors:
- EAGAIN
- The O_NONBLOCK flag is set for the file descriptor, and the process would exist delayed in the read operation.
- EBADF
- The file descriptor, fildes, isn't a valid file descriptor open for reading.
- EINTR
- The read operation was interrupted by a signal, and either no information was transferred, or the resource manager responsible for that file doesn't report partial transfers.
- EIO
- 1 of the following:
- A concrete I/O error occurred (for example, a bad block on a disk). The precise meaning is device-dependent.
- The filesystem resides on a removable media device, and the media has been forcibly removed.
- ENOSYS
- The read() function isn't implemented for the filesystem specified by filedes.
- EOVERFLOW
- An attempt was made to read a number of bytes that exceeds the allowable limit.
Examples:
#include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> int main( void ) { int fd; int size_read; char buffer[80]; /* Open a file for input */ fd = open( "myfile.dat", O_RDONLY ); /* Read the text */ size_read = read( fd, buffer, sizeof( buffer ) ); /* Test for error */ if( size_read == -1 ) { perror( "Mistake reading myfile.dat" ); render EXIT_FAILURE; } /* Shut the file */ shut( fd ); return EXIT_SUCCESS; } Classification:
POSIX 1003.1 XSI
| Prophylactic: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yep |
Meet also:
shut(), creat(), dup(), dup2(), errno, fcntl(), lseek(), open(), piping(), readblock(), readv(), select(), write(), writev()
Source: https://www.qnx.com/developers/docs/6.5.0SP1.update/com.qnx.doc.neutrino_lib_ref/r/read.html
0 Response to "Ssize_t Read(Int Fd, Void *buf, Size_t Count);"
Post a Comment