Linux---lseek () function

Source: Internet
Author: User

Each open file has a "current file offset" associated with it. It is usually a non-negative integer that measures the number of bytes computed from the beginning of the file. Typically, read and write operations start at the current file offset and increase the number of bytes read and written by the offset. By default, when you open a file, the offset is set to 0 unless you specify the O_append option.

The Lseek call only records the current file offset in the kernel, and it does not cause any I/O operations. The offset is then used for the next read or write operation.

Function prototypes

#include <unistd.h>

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

The parameter fildes is an open file descriptor, and the parameter offset is the number of displacements that move the read-write position according to the parameter whence.

Whence is one of the following: (Seek_set,seek_cur and Seek_end and sequentially 0, 1, and 2).

Seek_set the read-write position to the head of the file and then increases the offset displacement.

The seek_cur increases the offset displacement by the current read and write position.

Seek_end points the read and write position to the end of the file and then increments the offset displacement.

When the whence value is Seek_cur or seek_end, the parameter Offet allows negative values to appear.

The following are the more specific ways to use:

1) to move the read-write location to the beginning of the file:
Lseek (int fildes,0,seek_set);
2) to move the read-write location to the end of the file:
Lseek (int fildes,0,seek_end);
3) When you want to get the current file location:
Lseek (int fildes,0,seek_cur);

return value

When the call succeeds, it returns the current read-write location, which is the number of bytes from the beginning of the file. If there is an error, returning -1,errno will store the error code.

The error code for the Erron may be set:

Ebadf:fildes is not an open file descriptor.

Espipe: The file descriptor is assigned to a pipe, socket, or FIFO.

Einval:whence value is not properly evaluated.

Note: The Linux system does not allow Lseek () to act on the TTY device, which causes lseek () to return espipe.

Linux---lseek () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.