C language to get the file size sample _c language

Source: Internet
Author: User
Tags function prototype

1.fseek

Function Prototypes:

Copy Code code as follows:

int fseek (FILE * stream, long int offset, int origin);

Parameter description: Stream, file stream pointer, offest, offset, Orgin, original (beginning position). The optional values for Orgin are Seek_set (start of File), Seek_cur (current position of the file pointer), Seek_end (end of file).

Function Description: For binary mode open stream, the new stream position is Origin + offset.

2.ftell

Function prototype: Long int ftell (FILE * stream);

Function Description: Returns the position of the stream. For the binary stream, the return value is the number of bytes from the start position of the file.

Get file size C program (FILE.CPP):

Copy Code code as follows:

#include <stdio.h>

int main ()
{
FILE * PFILE;
Long size;

PFile = fopen ("File.cpp", "RB");
if (pfile==null)
Perror ("Error opening file");
Else
{
Fseek (pFile, 0, seek_end); Move file pointer to end of file
Size=ftell (PFile); Find the number of bytes from the current file pointer to the beginning of the file
Fclose (PFile);
printf ("Size of File.cpp:%ld bytes.\n", size);
}
return 0;
}

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.