Read many contents from the file

Source: Internet
Author: User

Usage of the fseek Function

Function Name: fseek
Function: relocate the file pointer on the stream
Usage: int fseek (File * stream, long offset, int fromwhere );
Program example:

# Include <stdio. h>

Long filesize (File * stream );

Int main (void)
{
File * stream;

Stream = fopen ("myfile. txt", "W + ");
Fprintf (stream, "this is a test ");
Printf ("filesize of myfile. txt is % LD Bytes/N", filesize (Stream ));
Fclose (Stream );
Return 0;
}

Long filesize (File * Stream)
{
Long curpos, length;

Curpos = ftell (Stream );
Fseek (stream, 0l, seek_end );
Length = ftell (Stream );
Fseek (stream, curpos, seek_set );
Return length;
}
========================================================== ======================================

Fseek () function usage

Call form:


# Include "stdio. H"

Fseek (file type pointer FP, displacement, starting point );

Function: Place the FP-Related File Location pointer to a specified location.

Where,"Displacement"Is long data, which indicates the number of bytes that the position pointer moves relative to the" starting point. If the displacement is a positive value, it indicates moving from the start point to the end of the file. If the displacement is a negative value, it indicates moving from the start point to the end of the file.

"Start PointIt can only be one of the three symbol constants defined in stdio. h:

Start Point

Corresponding number

File Location

Seek_set

0 File start

Seek_cur

1 Current File Location

Seek_end

2 End of File

For example:

Fseek (FP, 50l, 0); or fseek (FP, 50l, seek_set );

It moves the position pointer to 50 bytes away from the file header.


Note: After fseek (FP, 0l, seek_end), the current pointer of the file will be the last position, so if you need to read it, it will be read from there


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.