Linux system library function-lseek function usage

Source: Internet
Author: User

All open files have a current file offset (CFO. CFO is a non-negative integer used to indicate the number of bytes from the beginning of the file to the current position of the file. Read/write operations usually start with CFO, and increase CFO, increment to the number of read/write bytes. When the file is opened, CFO is initialized to 0 unless o_append is used.

You can use the lseek function to change the CFO of a file.

# Include

Off_t lseek (INT filedes, off_t offset, int whence );

Return Value: New offset (successful),-1 (failed)

The meaning of the parameter offset depends on the parameter whence:

1. If the whence is seek_set, the file offset will be set to offset.
2. If the whence is seek_cur, the file offset will be set to CFO plus offset,
Offset can be positive or negative.
3. If the whence is seek_end, the file offset will be set to the length of the file plus the offset,
Offset can be positive or negative.

Seek_set, seek_cur, and seek_end are introduced by System V. Before that, 0, 1, and 2 are used.

The following lseek usage returns the current offset:

Off_t currpos;
Currpos = lseek (FD, 0, seek_cur );

This technique can also be used to determine whether we can change the offset of a file. If the FD parameter specifies pipe, FIFO, or socket, lseek returns-1 and sets errno to espipe.

For a regular file, CFO is a non-negative integer. However, for special devices, CFO may be negative. Therefore, we cannot simply test whether the return value of lseek is less than 0 to determine whether lseek is successful or not. Instead, we should test whether the return value of lseek is equal to-1 to determine whether lseek is successful or not.

Lseek only saves CFO in the kernel and does not cause any I/O operations. This CFO will be used for subsequent read/write operations.

If the offset value is longer than the current file length, the file will be extend in the next write operation )". This is called the creation of "holes" in files )". All bytes that are not actually written to the file are represented by repeated 0 bytes. Whether an empty space occupies hard disk space is determined by the file system.

Original article: Workshop

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.