linux C 读取目录文件并统计文件数
来源:互联网 发布:淘宝已评论怎么看不到 编辑:程序博客网 时间:2024/06/17 18:29
转自:http://blog.csdn.net/hhbbhh/article/details/5192411
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#define MAX 1024
int get_file_count(char *root)
{
DIR *dir;
struct dirent * ptr;
int total = 0;
char path[MAX];
dir = opendir(root); /* 打开目录*/
if(dir == NULL)
{
perror("fail to open dir");
exit(1);
}
errno = 0;
while((ptr = readdir(dir)) != NULL)
{
//顺序读取每一个目录项;
//跳过“..”和“.”两个目录
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
{
continue;
}
//printf("%s%s/n",root,ptr->d_name);
//如果是目录,则递归调用 get_file_count函数
if(ptr->d_type == DT_DIR)
{
sprintf(path,"%s%s/",root,ptr->d_name);
//printf("%s/n",path);
total += get_file_count(path);
}
if(ptr->d_type == DT_REG)
{
total++;
printf("%s%s/n",root,ptr->d_name);
}
}
if(errno != 0)
{
printf("fail to read dir"); //失败则输出提示信息
exit(1);
}
closedir(dir);
return total;
}
int main(int argc, char * argv[])
{
int total;
if(argc != 2)
{
printf("wrong usage/n");
exit(1);
}
total = get_file_count(argv[1]);
printf("%s ha %d files/n",argv[1],total);
return 0;
}
- linux C 读取目录文件并统计文件数
- linux C 读取目录文件并统计文件数
- linux 统计目录下的文件数
- Linux使用C读取文件目录
- linux c 文件与目录读取
- C++ 读取文件并统计字符数、行数和单词数
- Linux统计文件、目录数量
- 20150123 【 Linux-C 文件目录操作 】 统计代码行数
- 统计目录下的文件数
- 读取磁盘中的文件,并统计行数
- linux统计文件、文件目录个数
- Linux统计文件目录及其文件个数
- Linux统计文件、目录个数,文件行数
- C读取文件并显示
- linux c 读取文件
- Python读取文件目录并检索
- linux c 读取并处理文件夹下的所有文件
- 分词统计(二)读取TXT文件并统计数量
- 应用程序池是个什么东东?
- C与C++的区别
- 【iOS开发-64】微博案例练习:用代码自定义cell,主要内容是针对不同高度的cell
- linux下编译php的amqp扩展(for rabbimq)
- Android一键退出整个应用程序
- linux C 读取目录文件并统计文件数
- QT中使用QXmlStreamReader解析XML文件
- Remove Duplicates from Sorted Array (Java)
- Jmeter自定义函数
- mysql sql语句大全
- [第一天]Win7环境配置Jdk 环境变量
- string与wstring转换
- Linux服务器Tomcat安全配置
- 武汉网站建设技术哪家强,武汉八音猫科技