c - C中带有nftw函数的目录递归路径

标签 c nftw

我正在尝试使用 nftw() 函数通过一些文件的总和来计算目录的大小。但是如果我不能使用全局变量来存储大小的总和,我如何处理总和,我必须使用函数 nftw() ?

最佳答案

我一直在研究,确实没有办法将参数传递给nftw函数来存储总和。但是我找到了一个解决方案,但我不知道它是否很好。

我的解决方案使用“嵌套函数”,如下所示:

int aux (char * name, int fd_limit, int flags) {

       int sum = 0;
       int nftwfunc (const char* filename, const struct stat* statptr, int fileflags, struct FTW* pfwt) {

           sum + = statptr-> st_size; 

       }

       nftw (name, nftwfunc, fd_limit, flags);
     
       return sum;

}

关于c - C中带有nftw函数的目录递归路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044443/

相关文章:

c - 在 Msp430 上使用 4 位气泡显示

c++ - C/C++ 巨大的头文件/源代码

使用 nftw() 遍历文件夹时检查级别变化

c - nftw 传递具有未定义值的 tflag

c - 使用 SAS proc proto 将字符串传递给 C 函数

c - 这些系统调用有什么问题?

c - 为什么会出现段错误?我正在使用 stat、mmap、nftw 和 memcmp 等

c - 在 c 中使用 ntfw 时跳过子目录

c - 在 C 的回调函数中 fork() 是否安全?

c - 使用 .cfg 文件的指针警告