Traverse all files in the directory

  Traverse all the file information under the directory, then the directory may contain subdirectories, if the directory is still open, then this subdirectory must be opened again. So this needs to use recursion, in fact, many traversals often need to use recursion, because there is more than one layer to traverse.

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);
      }
}

}
  Type 4 is a directory, then the full path of this subdirectory must be passed to the traversal function.

int ReadDir(const char *strpathname)
{ ReadDir(strchdpath); this is recursion }

A reminder that recursion is often used in traversal classes.

Guess you like

Origin blog.csdn.net/qq_43403759/article/details/113107267