如何用C语言获取文件的大小

2024-12-19 19:12:32
推荐回答(1个)
回答1:

两种方法:
1、用stat()函数来获取
int main()
{
struct stat st ;
stat( "file.txt", &st );
printf(" file size = %d\n", st.st_size);
return 0;
}

2、用ftell()函数来获取

int main()
{
FILE *fp;
fp=fopen( "file.txt", "r");
fseek(fp, 0L, SEEK_END );
printf(" file size = %d\n", ftell(fp) );
return 0;
}