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 ;}