linux获取目录下文件大小的问题

来源:互联网 发布:编程语言实现模式 代码 编辑:程序博客网 时间:2024/05/22 15:34

代码如下:

void list_dir(char *path)

{

DIR *dir;

struct dirent *entry;

struct stat stat_buf;

if ((dir = opendir(path)) == NULL)

{

printf("cannot open dir:%s\n", path);

return;

}

while ((entry = readdir(dir)) != NULL)

{

lstat(entry->d_name, &stat_buf);

if (!S_ISDIR(stat_buf.st_mode))

printf("%s, %d", entry->d_name, (int)stat_buf.st_size);

}

closedir(dir);

}

结果打印出来的是这种结果:

httpd-2.4.2.tar.gz, 4096

Firefox-latest.exe, 4096

Makefile.pdf, 4096

cr-SecureCRT.rar, 4096

putty.exe, 4096

结果显示大小都是4096,有时候只是其中一个文件的大小比如,都是xxx文件的大小,那个4096应该是目录"."".."的大小,这就很奇怪了,为什么打印出来的不是自身文件的大小。

后来没办法用以下方法获取文件大小:

FILE *fp = fopen(name, "r");

if (fp)

{

fseek(fp, 0L, SEEK_END);

int len = ftell(fp);

fseek(fp, 0L, SEEK_SET);

return len;

}

但是使用标准的c函数来获取文件大小有个问题,就是一般能够获取出小文件的大小,但是有的大文件就无法获取出大小,因为标准C的文件操作函数不支持对超过2G的文件读取

至于为什么用stat_buf.st_size获取的大小有问题,还没有搞清楚原因。

经过网上搜索及测试,终于发现了stat_buf.st_size获取的大小有问题的原因了。

原因解释:linux下目录保存的并不是文件本身,在 Linux 下,目录保存的实际是"文件名"+inodeinode 是到文件系统的元数据(文件的类型,大小,所有者,权限,等等)的索引。文件系统就好比一个大仓库,文件就好比保存在该仓库中的各种货物,而目录呢,就好比仓库门口的货物清单(及其在仓库中的位置等信息)。显然在清单里面并没有包含货物本身,而你获取清单本身的大小(就如同你获取目录的大小一样)同样无法获取清单里面所列物品的大小。 

所以上述用stat_buf.st_size获取目录下文件大小的代码需要作如下修改:

void list_dir(char *path)

{

DIR *dir;

struct dirent *entry;

struct stat stat_buf;

if ((dir = opendir(path)) == NULL)

{

printf("cannot open dir:%s\n", path);

return;

}

while ((entry = readdir(dir)) != NULL)

{

char tmp[100];

strcpy(tmp, path);

strcat(tmp, entry->d_name);

lstat(tmp, &stat_buf);

if (!S_ISDIR(stat_buf.st_mode)) {

printf("%s, %d", entry->d_name, (int)stat_buf.st_size);

}

}

closedir(dir);

}

原创粉丝点击