使用C计算目录中的文件数

老师:

我如何在Linux平台上使用C计算目录中的文件数。

米歇尔·布丁(Michiel Buddingh):

不保证此代码可以编译,并且实际上仅与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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章