遍历目录下的所有文件

  遍历目录下的所有文件信息,那么目录可能包含子目录,如果打开的还是目录,那么就要再将这个子目录打开。所以这给需要用到递归,其实很多的遍历是经常需要用到递归,因为遍历的不止有一层。

int ReadDir(const char *strpathname){
while(1)
{
if((stdinfo=readdir(dir))==0) break;

    if(strncmp(stdinfo->d_name,".",1)==0)  continue;  //以.开始的文件不读,这是隐藏文件
  
    if(stdinfo->d_type==8)  //如果是文件,显示出来
      printf("name=%s/%s\n",strpathname,stdinfo->d_name);

    if(stdinfo->d_type==4)
      {
         sprintf(strchdpath,"%s/%s",strpathname,stdinfo->d_name);
         ReadDir(strchdpath);
      }
}

}
  类型4就是目录,那么就要将这个子目录的全路径传给遍历的函数。

int ReadDir(const char *strpathname)
{
ReadDir(strchdpath);这个就是递归
}

提醒一下遍历类的经常用到递归。

猜你喜欢

转载自blog.csdn.net/qq_43403759/article/details/113107267
今日推荐