File I/O (without buffering) lseek Function

Source: Internet
Author: User

Each opened file has a "current file offset" associated with it ). It is usually a non-negative integer used to measure the number of bytes calculated from the beginning of the file. Generally, read and write operations start from the offset of the current file and increase the number of bytes read and write.

You can call lseek to set the offset for an opened file:

#include <unistd.h> filedes, off_t offset,  whence );

Return Value: if the operation succeeds, the new file offset is returned. If an error occurs, the value-1 is returned.

The description of the parameter offset is related to the value of the parameter whence. (.)

If whence is SEEK_SET, set the offset of the file to offset bytes from the beginning of the file.

If whence is SEEK_CUR, set the offset of the file to its current position and add offset. The offset can be positive or negative.

If whence is SEEK_END, the offset of the new file is set to the length of the file plus offset, and the offset can be positive or negative.

If lseek is successfully executed, a new file offset is returned. You can use the following method to determine the current offset of the opened file:

= lseek( fd, , SEEK_CUR );

 

This method can also be used to determine whether offsets can be set for the involved files. If the file descriptor references a pipe, FIFO, or network socket, lseek returns-1 and errno is set to ESPIPE.

The three symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END are introduced by system V. Before System V, whence is specified as 0 (absolute offset), 1 (offset relative to the current position), or 2 (offset relative to the end of the file ). Many software programs still write these numbers directly in the code.

The character l in lseek indicates a long integer. Before the off_t data type is introduced, the offset parameter and return value are long integers. Lseek was introduced by V7 and was added to the C language (in V6, The seek and tell functions are used to provide similar functions ).

Program list 3-1 test whether offset can be set for standard input

[root@localhost apue]# cat prog3-(lseek(STDIN_FILENO, , SEEK_CUR) == -

To call this program, you can:

[root@localhost apue]# ./prog3- < /etc//prog3- < /etc/

For questions about/etc/motd and/etc/issue, refer to: http://itchen.blog.51cto.com/343363/206679

Generally, the current offset of a file should be a non-negative integer, but some devices may also allow a negative offset. However, for normal files, the offset must be non-negative.

The/dev/kmem device running on the Intel x86 processor supports a negative offset.

Because the offset (off_t) is a signed data type, the maximum file length is halved. For example, if off_t is a 32-bit integer, the maximum length of the file is 2 ^ 31-1 bytes.

The file offset can be greater than the current length of the file. In this case, the next write to the file will lengthen the file and form a file, which is allowed. All bytes in the file but not written are read as 0.

. The specific processing method is related to the implementation of the file system. When writing beyond the end of the file is located, disk blocks need to be allocated for newly written data, however, you do not need to allocate disk blocks for the parts between the end of the original file and the new start write location.

Program list 3-2 create a file with Holes

[root@localhost apue]# cat prog3-<fcntl.h> buf1[] =  buf2[] = ((fd = creat(, FILE_MODE)) < (write(fd, buf1, ) !=         (lseek(fd, , SEEK_SET) == -        (write(fd, buf2, ) != 

Run the program to obtain:

[root@localhost apue]# ./prog3---rw-r--r--  root root  - :-   a   b   c   d   e   f   g   h   i   j  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \  \*

. The-c flag in the command indicates that the file content is printed as a character. We can see that the unwritten bytes in the middle of the file are read to 0. The first seven-digit value of each row is the byte offset in octal format.

Because the offset used by lseek is expressed by the off_t type, you can select an appropriate data type based on the specific platform. Currently, most platforms provide two sets of interfaces to process the file offset: one uses the 32-bit file offset, and the other uses the 64-bit file offset.

Note: Although 64-bit file offset is supported, whether to create a file larger than 2 GB (2 ^ 31-1 bytes) depends on the type of the underlying file system.

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.