#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
unsigned long long totle=0;
unsigned long long getfile(char* path)
{
char path_ch[512];
DIR * dir=NULL;
struct dirent * prt=NULL;
// printf("second:%s\n",path);
dir=opendir(path);
if(dir==NULL)
{
printf("%lld",totle);
perror("open error");
exit(1);
}
//循环读目录
while(prt=readdir(dir))
{
//判断是不是“.”或“..”目录
if(strcmp(".",prt->d_name)==0||strcmp("..",prt->d_name)==0)
{
continue;
}
//判断文件类型是否为普通文件
if(prt->d_type==DT_REG)
{
printf("%s\n",prt->d_name);
totle++;
}
}
//判断文件类型是否为目录
if(prt->d_type==DT_DIR)
{
// memset(path_ch,0,sizeof(path_ch));
sprintf(path_ch,"%s/%s",path,prt->d_name);
getfile(path_ch);
}
}
closedir(dir);
return totle;
}
int main(int argc,char * argv[])
{
// char* root_path=argv[1];
// printf("first:%s\n",argv[1]);
printf("%lld",getfile(argv[1]));
return 0;
}
linux opendir()、readdir()函数遍历目录,获取文件名和数量
最新推荐文章于 2022-07-30 17:22:28 发布