用stat()或在文件打开后用fstat()。
这两个调用会将文件信息填入一个结构中, 其中你能找到诸如文件主人、属性、 大小、最后访问时间、最后修改时间等所有关于此文件的东西。
下面的程序大体示范如何用stat()得到文件大小。
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int get_file_size(char *path,off_t *size)
{
struct stat file_stats;
if(stat(path,&file_stats))
return -1;
*size = file_stats.st_size;
return 0;
}