#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<dirent.h>
int get(char* root)
{
int tot=0;
//open dir
DIR* dir=NULL;
dir=opendir(root);
if(dir==NULL)
{
perror("opendir");
exit(1);
}
//遍历当前打开的目录
struct dirent* ptr=NULL;
while((ptr=readdir(dir)))
{
//过滤. 和..
if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)
continue;
//如果是目录
if(ptr->d_type==DT_DIR)
{
char path[1024];
sprintf(path,"%s/%s",root,ptr->d_name);
tot+=get(path);
}
//如果是文件
if(ptr->d_type==DT_REG)
tot++;
}
closedir(dir);
return tot;
}
int main(int argc,char* argv[])
{
if(argc<2)
{
printf("./a.out dir\n");
exit(1);
}
printf("%d\n",get(argv[1]));
return 0;
统计文件个数
最新推荐文章于 2023-09-13 17:22:45 发布