linux c文件位置,Linux C程序实现文件夹大小提取

在嵌入式Linux系统中,经常要对一些实时数据进行存储,而在存储空间有限的情况下往往需要判断存储目录中的文件夹的大小,而通过C语言实现文件夹大小的获取在网上的程序可是少之又少,现提供一个程序,大家一起分享,分享,其实程序是提取文件夹下所有文件大小,提取运行程序文件夹下的文件的大小之和,但不包括文件夹目录下的文件夹的大小。

体程序如下:

#include

#include

#include

main()

{

DIR *d;

struct dirent *de;

struct stat buf;

int exists;

int total_size;

d = opendir(".");

if (d == NULL) {

perror("prsize");

exit(1);

}

total_size = 0;

for (de = readdir(d); de != NULL; de = readdir(d)) {

exists = stat(de->d_name, &buf);

if (exists < 0) {

fprintf(stderr, "Couldn't stat %s\n", de->d_name);

} else {

total_size += buf.st_size;

}

}

closedir(d);

printf("%d\n", total_size);

}

以下为另外一个文件夹大小提取程序,程序内容:

#include

#include

#include

#include

#include

static unsigned int total = 0;

int sum(const char *fpath, const struct stat *sb, int typeflag)

{

total += sb->st_size;

return 0;

}

int main(int argc, char **argv)

{

if (!argv[1] || access(argv[1], R_OK)) {

return 1;

}

if (ftw(argv[1], &sum, 1)) {

perror("ftw");

return 2;

}

printf("%s: %u\n", argv[1], total);

return 0;

}

通过GCC编译程序

gcc -o dir_size dir_size.c

运行程序

./dir_size /licy/0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值