Name

lseek - repositions read/write file offset

Library

libc.lib

Synopsis

  #include <unistd.h>
  off_t lseek (int fildes, off_t offset, int whence);

Return values

Upon successful completion, lseek returns the resulting offset location as measured in bytes from the beginning of the file. Otherwise, a value of -1 is returned and errno is set to indicate the error.

Detailed description

The lseek system call repositions the offset of the file descriptor fildes to the argument offset according to the directive whence. The argument fildes must be an open file descriptor. The lseek system call repositions the file position pointer associated with the file descriptor fildes as follows:
If whence is SEEK_SET, the offset is set to offset bytes.
If whence is SEEK_CUR, the offset is set to its current location plus offset bytes.
If whence is SEEK_END, the offset is set to the size of the file plus offset bytes.
Some devices are incapable of seeking. The value of the pointer associated with such a device is undefined.

Note : lseek function allows the file offset to be set beyond the existing end-of-file, data in the seeked slot is undefined, and hence the read operation in seeked slot is undefined untill data is actually written into it. lseek beyond existing end-of-file increases the file size accordingly.


Examples

/**
  * Detailed description  : Example for lseek usage.
**/
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int fd = 0;
 fd = open("lseek.txt"  , O_CREAT | O_RDWR , 0666);
  if(lseek(fd , 0 , SEEK_SET) < 0 ) {
     printf("Lseek on file lseek.txt failed \n");
      return -1;
  }
  printf("Lseek on lseek.txt passed ");
 return 0;
}

         

Output

Lseek on lseek.txt passed

         


Errors

The lseek system call will fail and the file position pointer will remain unchanged if:
[EBADF]
  The fildes argument is not an open file descriptor.
[EINVAL]
  The whence argument is not a proper value or the resulting file offset would be negative for a non-character special file.
[EOVERFLOW]
  The resulting file offset would be a value which cannot be represented correctly in an object of type off_t(Not supported).
[ESPIPE]
  The fildes argument is associated with a pipe, socket, or FIFO.

See also

dup, open

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top