计数使用℃的目录中的文件的数目 [英] Counting the number of files in a directory using C

查看:126
本文介绍了计数使用℃的目录中的文件的数目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I count the number of files in a directory using C on linux platform.

推荐答案

没有保证,这code编译,而且它真的只有Linux和BSD系统兼容:

No guarantee that this code compiles, and it's really only compatible with Linux and the BSDs:

#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);

这篇关于计数使用℃的目录中的文件的数目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆