我如何在Linux平台上使用C计算目录中的文件数。
不保证此代码可以编译,并且实际上仅与Linux和BSD兼容:
#include <dirent.h>
...
int file_count = 0;
DIR * dirp;
struct dirent * entry;
dirp = opendir("path"); /* There should be error handling after this */
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
file_count++;
}
}
closedir(dirp);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句