How to get the file size through ftell and fseek

Source: Internet
Author: User

# Include <stdio. h> FILE * pFile = fopen (, (pFile = NULL) perror (fseek (pFile, size = pFile = printf (}

First, use fseek (pfile, 0, SEEK_END) to move the current position of the file to the end of the file, and then call the ftell () function to obtain the current position relative to the first position of the file, this displacement value is equal to the number of bytes contained in the file. In this way, the file size is obtained!

Fseek (relocate the file pointer on the Stream)

Int fseek (FILE * stream, long offset, int fromwhere );

Function Description: The first parameter stream is the file pointer, the second parameter offset is the offset, the integer indicates the positive offset, and the negative value indicates the negative offset. The third parameter fromwhere sets the offset starting from the file, possible values: SEEK_CUR (current location), SEEK_END (end of the file), or SEEK_SET (start of the file). SEEK_SET, SEEK_CUR, and SEEK_END are values 0, 1 and 2 in sequence.
For example:
Fseek (fp, 100L, 0); move the fp pointer to the first 100 bytes from the file;

Return Value: success. 0 is returned. Otherwise, other values are returned.

Ftell (returns the current file location)

Long ftell (FILE * stream)

Function Description: obtains the number of offset bytes between the current position of the file pointer and the beginning of the file.

Related Article

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.