Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of lseek () function and fseek () function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

C language lseek () function: move the read and write location of the file

Header file:

# include # include

Define the function:

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

Function description:

Every open file has a read-write location. When opening a file, the read-write location usually points to the beginning of the file. If you open the file in an additional way (such as O_APPEND), the read-write location points to the end of the file. When read () or write (), the read-write position increases, and lseek () is used to control the read-write location of the file. The parameter fildes is the open file description, and the parameter offset is the number of displacements that move the read and write position according to the parameter whence.

The parameter whence is one of the following:

The SEEK_SET parameter offset is the new read and write location.

SEEK_CUR increases the number of offset shifts back from its current read and write position.

SEEK_END points the read and write location to the end of the file and then increases the offset displacement. When the whence value is SEEK_CUR or

When SEEK_END, the parameter offet allows negative values to appear.

The following are special ways to use teaching:

1) when you want to move the read / write location to the beginning of the file: lseek (int fildes, 0, SEEK_SET)

2) when you want 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 is successful, it returns the current read and write position, that is, how many bytes from the beginning of the file. Return-1 if there is an error, and errno will store the error code.

Additional note: the Linux system does not allow lseek () to act on the tty device, this action will make lseek () return ESPIPE.

C language fseek () function: move the read and write location of the file stream

Header file:

# include

Define the function:

Int fseek (FILE * stream, long offset, int whence)

Function description:

Fseek () is used to move the read and write location of the file stream.

1. The parameter stream is the pointer to the open file.

2. The parameter offset is the number of displacements that move the read and write position according to the parameter whence. The parameter whence is one of the following:

The SEEK_SET displacement from the beginning of the file offset to the new read and write location. SEEK_CUR increases the number of offset shifts back from its current read and write position.

SEEK_END points the read and write location to the end of the file and then increases the offset displacement. When the whence value is SEEK_CUR or

When SEEK_END, the parameter offset allows negative values to appear.

The following are more special ways to use:

1) when you want to move the read / write location to the beginning of the file: fseek (FILE * stream, 0, SEEK_SET)

2) when you want to move the read / write location to the end of the file: fseek (FILE * stream, 0, 0SEEK_END)

Return value: 0 is returned when the call is successful,-1 is returned if there is an error, and errno stores the error code.

Additional note: fseek () does not return the read-write position like lseek (), so you must use ftell () to get the current read-write position.

Example

# include main () {FILE * stream; long offset; fpos_t pos; stream = fopen ("/ etc/passwd", "r"); fseek (stream, 5, SEEK_SET); printf ("offset =% d\ n", ftell (stream)); rewind (stream); fgetpos (stream, & pos); printf ("offset =% d\ n", pos); pos = 10; fsetpos (stream, & pos) Printf ("offset =% d\ n", ftell (stream)); fclose (stream);}

Execution

Offset = 5offset = 0offset = 10

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report