The use of the Lseek function __ function

Source: Internet
Author: User
Tags printf

All open files have a current file offset, which is referred to as the CFO. The CFO is typically a non-negative integer that indicates the number of bytes at the beginning of the file to the current location of the file. Read and write operations typically start with the CFO and increase the CFO by the number of bytes read and write. When the file is opened, the CFO is initialized to 0 unless O_append is used.

Use the Lseek function to change the file's CFO.

#include <unistd.h>

#include <sys/types.h>

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

Return value: New offset (success), 1 (failed)

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

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

Seek_set, Seek_cur, and Seek_end were introduced by System V, before which 0, 1, and 2 were used.

The following usage of Lseek returns the current offset:

off_t Currpos;
Currpos = Lseek (FD, 0, seek_cur);

This technique can also be used to determine if we can change the offset of a file. If the parameter fd (file descriptor) specifies a pipe (pipe), FIFO, or Socket,lseek returns 1 and the errno is espipe.

For normal files (regular file), the CFO is a non-negative integer. For special equipment, however, the CFO may be negative. Therefore, we can not simply test whether the return value of Lseek is less than the Lseek success or not, but should test whether the return value of Lseek is equal-to determine lseek success or not.

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

If offset is larger than the current length of the file, the next write operation will "extend" the file. This is called creating a "void (hole)" in a file. All bytes that are not actually written to the file are represented by a duplicate of 0. Whether the hole occupies hard disk space is determined by the file system.

The following program creates an empty file:

/* Standard C Header */
#include <stdio.h>
/* Unix Header */
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

Char buf1[] = "ABCDEFGHIJ";
Char buf2[] = "ABCDEFGHIJ";

int main (void)
{
int fd, size;

if (fd = creat ("File.hole", s_irusr| S_IWUSR)) < 0)
{
printf ("creat error\n");
return-1;
}

            size = sizeof buf1-1;
            if (write (fd, BUF1, size)! = size)
 & nbsp;          {
                 printf ("Buf1 write error\n");
                return-1;
           }
           /* Offset now = ten */

            if (Lseek (FD, 16384, seek_set) = =-1)
            {
                 printf ("Lseek error\n");
                return-1;
           }
           /* Offset now = 16384 */

size = sizeof buf2-1;
if (Write (fd, BUF2, size)! = size)
{
printf ("Buf2 write error\n");
return-1;
}
/* Offset now = 16394 */

return 0;
}

Excerpt from: http://apps.hi.baidu.com/share/detail/30893139

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.