How to obtain the file size in C

Source: Internet
Author: User

Author: Qing Dujun


Method 1:

Two functions are required to obtain the file size: fseek () and ftell ()

Fseek () function:

Prototype:IntFseek (FILE* Stream,LongOffset,IntFromwhere );

Parameters:

Stream: The first parameter stream is the file pointer.
Offset: The second parameter offset is the offset. A positive value indicates the positive offset and a negative value indicates the negative offset.
Fromwhere: The third parameter origin is set to start offset from the file, which may be SEEK_CUR, SEEK_END, or SEEK_SET.

SEEK_SET: Start of the file
SEEK_CUR: current location
SEEK_END: End of the file
SEEK_SET, SEEK_CUR, and SEEK_END are respectively 0, 1 and 2.
In short:
Fseek (fp, 100L, 0); move the internal pointer of the file to 100 bytes away from the beginning of the file;
Fseek (fp, 100L, 1); move the internal pointer of the file to 100 bytes away from the current position of the file;
Fseek (fp,-100L, 2); returns the internal pointer of the file to the first byte from the end of the file.

Reference: Baidu encyclopedia, fseek (), http://baike.baidu.com/view/656696.htm


Ftell () function:

Prototype:LongFtell (FILE* Stream );

Function: returns the current position of the FILE, that is, the current position of the FILE pointer. (That is, the number of first characters that deviate from the file)

Reference: Baidu encyclopedia, ftell (), http://baike.baidu.com/view/656699.htm

# Include
 
  
# Include
  
   
// Exit (0); int main () {FILE * fp = NULL; long n = 0; if (fp = fopen ("Q.txt", "r ")) = NULL) {printf ("file opening failed. \ n "); exit (0);} fseek (fp,); // pointer: Move to the end of the file n = ftell (fp ); // return the position of the pointer that deviates from the file header (that is, the number of characters in the file) printf ("% ld \ n", n); return 0 ;}
  
 

Method 2:

Used hereStruct _ statStruct

St_atime: Time when the file was last accessed

St_ctime: File Creation Time

St_dev: Disk of the file

St_mtime: Last file modification time

St_size: File Size

// All parameters can be found in MSDN

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Int main () {struct _ stat buf; // create a struct int result; result = _ stat ("Q.txt", & buf); printf ("file size: % ld \ n ", buf. st_size); printf ("Disk: % c: \ n", buf. st_dev + 'A'); printf ("Creation Time: % s", ctime (& buf. st_atime); 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.