简体   繁体   English

C语言中带有nftw函数的目录的递归路径

[英]Recursive path of a directory with the nftw function in C

I'm trying to use the nftw() function to calculate the size of a directory by the sum of some of the files. 我正在尝试使用nftw()函数通过某些文件的总和来计算目录的大小。 But how I do handle the sum if I cannot use global variables to store the sum of the sizes, and I have to use the function nftw() ? 但是如果我不能使用全局变量来存储大小的总和,我如何处理总和,我必须使用函数nftw()

I've been investigating, and it is true that there is no way to pass a parameter to the nftw function to store the sum. 我一直在调查,确实没有办法将参数传递给nftw函数来存储总和。 But I've found a solution, but I do not know if it is very good. 但我找到了解决方案,但我不知道它是否非常好。

My solution uses a "nested function" like the following: 我的解决方案使用“嵌套函数”,如下所示:

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM