#include <sys/types.h>
|
#include <sys/uio.h>
|
#include <unistd.h>
|
ssize_t
read (int d, void *buf, size_t nbytes);
|
ssize_t
readv (int d, const struct iovec *iov, int iovcnt); |
For readv the iovec structure is defined as:
struct iovec { void *iov_base; /* Base address. */ size_t iov_len; /* Length. */ };
Each iovec entry specifies the base address and length of an area in memory where data should be placed. The readv system call will always fill an area completely before proceeding to the next.
On objects capable of seeking, the read starts at a position given by the pointer associated with d (see lseek Upon return from read, the pointer is incremented by the number of bytes actually read.
Objects that are not capable of seeking always read from the current position. The value of the pointer associated with such an object is undefined.
Upon successful completion, read, and readv return the number of bytes actually read and placed in the buffer. The system guarantees to read the number of bytes requested if the descriptor references a normal file that has that many bytes left before the end-of-file, but in no other case.
/************************************************************************************ * Detailed description :This example demonstrates usage of read-system call, this * Example reads 10 bytes from a file specified * * Preconditions: Example.txt file should be present in C: and should contain * string Hello World. * *************************************************************************************** / #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd = 0; char Buff[12] = {0}; fd = open("C:\\Example.txt" , O_RDONLY ); if(fd < 0 ) { printf("Failed to open C:\\Example.txt file\n"); return -1; } if(read(fd , Buff , 11) < 11) { printf("Failed to read specified number of bytes from file\n"); return -1; } printf("file contains %s \n" , Buff); return 0; }
Output
file contains Hello World
/** * Detailed description: Sample usage of readv system call * Preconditions: Example.txt file should be present in working directory containing Hello world in it * **/ #include <stdio.h> #include <sys/types.h> #include <sys/uio.h> #include <fcntl.h> int main() { int fd = 0; struct iovec io_vec[1]; char Buf[12] = { 0 }; io_vec[0].iov_base = Buf; io_vec[0].iov_len = 11; fd = open("Example.txt" , O_RDONLY ); if(fd < 0 ) { printf("File open failed \n"); return -1; } if(readv(fd , io_vec , 1) < 11 ) { printf("Failed to read fron Example.txt file \n"); return -1; } printf("Read succes %s \n" , io_vec[0].iov_base); return 0; }
Output
Read succes Hello World
[EBADF] | |
The d argument is not a valid file or socket descriptor open for reading. | |
[ECONNRESET] | |
The d argument refers to a socket, and the remote socket end is forcibly closed. | |
[EFAULT] | |
The buf argument points outside the allocated address space(Not supported). | |
[EIO] | An I/O error occurred while reading from the file system(Not supported). |
[EINTR] | |
A read from a slow device was interrupted before any data arrived by the delivery of a signal(Not supported). | |
[EINVAL] | |
The pointer associated with d was negative. | |
[EAGAIN] | |
The file was marked for non-blocking I/O, and no data were ready to be read. | |
[EISDIR] | |
The file descriptor is associated with a directory residing on a file system that does not allow regular read operations on directories (e.g. NFS)(Not supported). | |
[EOPNOTSUPP] | |
The file descriptor is associated with a file system and file type that do not allow regular read operations on it(Not supported). | |
[EOVERFLOW] | |
The file descriptor is associated with a regular file, nbytes is greater than 0, offset is before the end-of-file, and offset is greater than or equal to the offset maximum established for this file system(Not supported). | |
[EINVAL] | |
The value nbytes is greater than INT_MAX. | |
In addition, readv and preadv may return one of the following errors:
[EINVAL] | |
The iovcnt argument was less than or equal to 0, or greater than IOV_MAX. | |
[EINVAL] | |
One of the iov_len values in the iov array was negative. | |
[EINVAL] | |
The sum of the iov_len values in the iov array overflowed a 32-bit integer. | |
[EFAULT] | |
Part of the iov array points outside the process’s allocated address space. | |
© 2005-2007 Nokia |