How to Get File Size
I want to know the file size. Could you tell me a function for that?
There is no function in C to determine file size.
To determine the file size, you need to open the file, move the file pointer, and then retrieve the size.
`fpos_t` is a variable representing the size of data, and it is essentially an unsigned integer value.Converting it to an int won't be a big deal.
Also, the file pointer, or the current read position, changes during the process described above, so it needs to be reset as follows.
To determine the file size, you need to open the file, move the file pointer, and then retrieve the size.
sum.c
fpos_t GetFileSize(const char* FileName[])
{
fpos_t fsize = 0;
FILE* fp = fopen(FileName, "rb");
/* Investigate file size */
fseek(fp, 0, SEEK_END);
fgetpos(fp, &fsize);
fclose(fp);
return fsize;
}
`fpos_t` is a variable representing the size of data, and it is essentially an unsigned integer value.Converting it to an int won't be a big deal.
Also, the file pointer, or the current read position, changes during the process described above, so it needs to be reset as follows.
sum.c
fpos_t fsize = 0;
fpos_t fsizeb = fseek(fp,0,SEEK_END);
fgetpos(fp,&fsize);
fseek(fp,fsizeb,SEEK_SET);
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




