Fseek (read/write location of a mobile file Stream)
Related functions: rewind, ftell, fgetpos, fsetpos, and lseek
Header file # include <stdio. h>
Defines the int fseek (FILE * stream, long offset, int whence) function );
Function Description: fseek () is used to move the read/write location of a file stream. The stream parameter is an opened file pointer, And the offset parameter is the number of read/write locations to be moved Based on the whence parameter.
The whence parameter is one of the following:
The offset displacement from the beginning of the SEEK_SET file is the new read/write location. SEEK_CUR increases the offset displacement at the current read/write position.
SEEK_END points the read/write position to the end of the file and then increases the offset displacement.
When the whence value is SEEK_CUR or SEEK_END, the offset parameter allows negative values.
The following are special usage methods:
1) to move the read/write location to the beginning of the FILE: fseek (FILE * stream, 0, SEEK_SET );
2) to move the read/write location to the end of the FILE: fseek (FILE * stream, 0, 0SEEK_END );
Return Value: if the call is successful, 0 is returned. If an error exists,-1 is returned. errno stores the error code.
Note that fseek () does not return the read/write location like lseek (). Therefore, ftell () must be used to obtain the current read/write location.
Example # include <stdio. h>
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 );
}
Execute offset = 5
Offset = 0
Offset = 10