[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    lseek(2) manual page Table of Contents

Name

lseek - move read/write file pointer

Synopsis


#include <sys/types.h>
#include <unistd.h>

off_t lseek(int fildes, off_t offset, int whence);

MT-Level

Async-Signal-Safe

Description

lseek() sets the file pointer associated with the open file descriptor specified by fildes as follows:

On success, lseek() returns the resulting pointer location, as measured in bytes from the beginning of the file. Note that if fildes is a remote file descriptor and offset is negative, lseek() returns the file pointer even if it is negative.

lseek() allows the file pointer to be set beyond the existing data in the file. If data are later written at this point, subsequent reads in the gap between the previous end of data and the newly written data will return bytes of value 0 until data are written into the gap.

Return Values

Upon successful completion, the resulting file pointer is returned. Remote file descriptors are the only ones that allow negative file pointers. Otherwise, a value of -1 is returned and errno is set to indicate the error.

Errors

lseek() fails and the file pointer remains unchanged if one or more of the following are true:

EBADF
fildes is not an open file descriptor.
EINVAL
whence is not SEEK_SET, SEEK_CUR, or SEEK_END.
EINVAL
fildes is not a remote file descriptor, and the resulting file pointer would be negative.
ESPIPE
fildes is associated with a pipe or fifo.

Some devices are incapable of seeking. The value of the file pointer associated with such a device is undefined.

See Also

creat(2) , dup(2) , fcntl(2) , open(2) , read(2) , write(2)

Notes

In multithreaded programs, using lseek() in conjunction with a read() or write() on a file descriptor shared amongst more than one thread is not an atomic operation. To ensure atomicity, use pread() or pwrite().


Table of Contents