实际上,之前博文中已经有两处用到了,现总结一下:
#include <stdio.h>
#include <time.h>
#include <sys\stat.h>
int getFileSize1(char str[])
{
FILE *fp = fopen(str, "rb");
fseek( fp, 0, SEEK_END );
int size = ftell(fp);
fclose(fp);
return size;
}
int getFileSize2(char str[])
{
FILE *fp = fopen("1.txt", "r");
struct stat fileStat;
fstat(fileno(fp), &fileStat);
return fileStat.st_size;
}
int main()
{
printf("%d\n", getFileSize1("1.txt"));
printf("%d\n", getFileSize2("1.txt"));
return 0;
}
结果和“属性”中显示的结果是一样的。