C language file locating Functions

Source: Internet
Author: User

The file has a position pointer pointing to the current read/write position. During the sequential read/write of a file, each read/write character automatically slides to the next character position. You can move the pointer to read and write data immediately. Below are several functions related to file location.

 

1. Void rewind (File * FP );

FP is the file pointer. If the returned value is void, no value is returned.

This function enables the position pointer to return the beginning of the file again.

 

2. Int Feek (File * FP, llong offset, int whence );

FP is the file pointer;

Offset displacement: the number of bytes that slide forward based on the starting point. If the displacement is smaller than 0, the system slides backward. Ansi c and most C versions require long data displacement. In this way, no problem occurs when the file length is larger than 64 KB. The ansi c standard requires that an English letter (l) be added to the end of a number to indicate a long character.

Whence start point can be represented by numbers 0, 1, 2, or macro names

The starting point name is represented by numbers.

File start SEEK_SET 0

Current File Location SEEK_CUR 1

Seek_end 2 at the end of the file

 

In addition, fseek is generally used for binary files. text files are often confused when character replacement occurs.

Example: fseek (FP, 100l, 0); fseek (FP, 30l, 1); fseek (FP,-30l, seek_end );

The returned value of fseek () is:

If 0 is returned successfully, nothing is returned, and no one says it on the Internet. The following is an English explanation. You can't understand it! Who can talk about it?

If successful, fseek and _ fseeki64 returns 0. Otherwise, it returns
A nonzero value. On devices incapable of seeking, the return value is undefined. If stream is
A null pointer, or if origin is not one of allowed values described below, fseek and _ fseeki64 invoke
The invalid parameter handler, as described in Parameter Validation. If
Execution is allowed to continue, these functions set errno to EINVAL and return
-1.

 

3. If long ftell (FILE * fp) successfully returns the position of the pointer to the current FILE, the return value is of the long type. Otherwise,-1 is returned and the global variable errno is set to indicate the error type.

 

 

Fseek and ftell can be combined to query the length of a file. First use fseek to locate the file position pointer to the end of the file, and then use ftell to query the size of the current file position pointer to get the file length.

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.