The Lseek function in file operation detailed _header

Source: Internet
Author: User

All open files have a current file offset, hereinafter referred to as the CFO. The CFO is usually a non-negative integer that indicates the number of bytes at the beginning of the file to the current location of the file. Read-write operations usually start with the CFO, and the CFO is incremented to the number of bytes read and written. When the file is opened, the CFO is initialized to 0 unless the o_append is used.
Use the Lseek function to change the CFO of the file.
#include <unistd.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 the file length plus offset,
Offset can be positive or negative.
Seek_set, Seek_cur and Seek_end were introduced by System V, which used to be 0, 1, and 2.
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 whether we can change the offset of a file. If the parameter fd (file descriptor) specifies the pipe (pipe), FIFO, or Socket,lseek return-1 and place errno as Espipe.
For normal files (regular file), the CFO is a non-negative integer. But for special equipment, the CFO may be negative. Therefore, we cannot simply test whether the return value of the Lseek is less than the 0来 judgment Lseek success, and should test whether the Lseek return value equals-to determine if Lseek is successful or not.
Lseek only keeps 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 the offset is larger than the current length of the file, the next write will "brace (extend)" the file. This is called the creation of "hole" in the document. All bytes that are not actually written to the file are represented by a duplicate of 0. Whether or not a hole occupies hard disk space is determined by the filesystem (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)
{
printf ("Buf1 write error\n");
return-1;
}
/* Offset now = 10 */
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;
}

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.