递归调用
遍历目录和子目录
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void DIRENT(char *path)
{
int len=0;
char str_stu[1024];
struct dirent *dir_file;
DIR *dir=opendir(path);
assert(dir!=NULL);
while((dir_file=readdir(dir))!=NULL)
{
if(strcmp(dir_file->d_name,".")==0||strcmp(dir_file->d_name,"..")==0) continue;
printf("%s\n",dir_file->d_name);
}
rewinddir(dir);
while((dir_file=readdir(dir))!=NULL)
{
if(strcmp(dir_file->d_name,".")==0||strcmp(dir_file->d_name,"..")==0) continue;
if(DT_DIR==dir_file->d_type)
{
len=strlen(path);
strcpy(str_stu,path);
if(str_stu[len-1]!='/')
strcat(str_stu,"/");
strcat(str_stu,dir_file->d_name);
printf("目录%s\n",str_stu);
DIRENT(str_stu);
}
}
closedir(dir);
}
int main(int argc, char **argv)
{
DIRENT(argv[1]);
return 0;
}