我正在尝试使用 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/