Node:I/O Primitives, Next:File Position Primitive, Previous:Opening and Closing Files, Up:Low-Level I/O
This section describes the functions for performing primitive input and
output operations on file descriptors: read
, write
, and
lseek
. These functions are declared in the header file
unistd.h
.
ssize_t | Data Type |
This data type is used to represent the sizes of blocks that can be
read or written in a single operation. It is similar to size_t ,
but must be a signed type.
|
ssize_t read (int filedes, void *buffer, size_t size) | Function |
The read function reads up to size bytes from the file
with descriptor filedes, storing the results in the buffer.
(This is not necessarily a character string, and no terminating null
character is added.)
The return value is the number of bytes actually read. This might be less than size; for example, if there aren't that many bytes left in the file or if there aren't that many bytes immediately available. The exact behavior depends on what kind of file it is. Note that reading less than size bytes is not an error. A value of zero indicates end-of-file (except if the value of the
size argument is also zero). This is not considered an error.
If you keep calling If In case of an error,
Please note that there is no function named This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time The |
ssize_t pread (int filedes, void *buffer, size_t size, off_t offset) | Function |
The pread function is similar to the read function. The
first three arguments are identical, and the return values and error
codes also correspond.
The difference is the fourth argument and its handling. The data block
is not read from the current position of the file descriptor
When the source file is compiled with The return value of
The function is an extension defined in the Unix Single Specification version 2. |
ssize_t pread64 (int filedes, void *buffer, size_t size, off64_t offset) | Function |
This function is similar to the pread function. The difference
is that the offset parameter is of type off64_t instead of
off_t which makes it possible on 32 bit machines to address
files larger than 2^31 bytes and up to 2^63 bytes. The
file descriptor filedes must be opened using open64 since
otherwise the large offsets possible with off64_t will lead to
errors with a descriptor in small file mode.
When the source file is compiled with |
ssize_t write (int filedes, const void *buffer, size_t size) | Function |
The write function writes up to size bytes from
buffer to the file with descriptor filedes. The data in
buffer is not necessarily a character string and a null character is
output like any other character.
The return value is the number of bytes actually written. This may be
size, but can always be smaller. Your program should always call
Once In the case of an error,
Unless you have arranged to prevent nbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count)); Please note that there is no function named This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time The |
ssize_t pwrite (int filedes, const void *buffer, size_t size, off_t offset) | Function |
The pwrite function is similar to the write function. The
first three arguments are identical, and the return values and error codes
also correspond.
The difference is the fourth argument and its handling. The data block
is not written to the current position of the file descriptor
When the source file is compiled with The return value of
The function is an extension defined in the Unix Single Specification version 2. |
ssize_t pwrite64 (int filedes, const void *buffer, size_t size, off64_t offset) | Function |
This function is similar to the pwrite function. The difference
is that the offset parameter is of type off64_t instead of
off_t which makes it possible on 32 bit machines to address
files larger than 2^31 bytes and up to 2^63 bytes. The
file descriptor filedes must be opened using open64 since
otherwise the large offsets possible with off64_t will lead to
errors with a descriptor in small file mode.
When the source file is compiled using |