Linux functions-fseek

Source: Internet
Author: User
Tags rewind

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

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.