Chinaunix

标题: 我的linux代码不能遍历所有目录,求助大神们 [打印本页]

作者: x6666f    时间: 2011-08-16 16:03
标题: 我的linux代码不能遍历所有目录,求助大神们
我的linux代码不能遍历所有目录,求助
代码很简短,不知为何却不能遍历所有目录,请大神看看
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. #include<dirent.h>
  5. void dirls(DIR *dirp,int num);
  6. int main(int argc,char **argv)
  7. {
  8.   ssize_t readnum;
  9.   DIR *dirp;
  10.   struct dirent *direntp;
  11.   if(argc<2)
  12.   return 0;
  13.   dirp=opendir(argv[1]);
  14.   dirls(dirp,1);
  15.   return 0;
  16. }
  17. void dirls(DIR *dirp,int num)
  18. {
  19.   struct dirent *direntp;
  20.   while(NULL != (direntp=readdir(dirp)))
  21.    {
  22.      DIR *dir;
  23.      fprintf(stdout,"%d           %s \n",num,direntp->d_name);
  24.      if(0!=strcmp(direntp->d_name,".")&&0!=strcmp(direntp->d_name,"..")&&NULL!=(dir=opendir(direntp->d_name)))
  25.      {
  26.        fprintf(stdout,"\n***********************%s*******************************\n",direntp->d_name);
  27.        dirls(dir,num+1);
  28.        fprintf(stdout,"\n#######################%s##################################\n",direntp->d_name);
  29.      }
  30.     }
  31.   closedir(dirp);
  32. }
复制代码

作者: linuxqie    时间: 2011-08-16 16:10
建议用递归实现!~~~
作者: jiayanfu    时间: 2011-08-16 17:11
本帖最后由 jiayanfu 于 2011-08-16 17:46 编辑

原因在于楼主对于opendir的参数不对,打开上级或者下级的目录的时候,那个路径给的不对。采用相对路径的话,可能稍微复杂点。。。如果采用绝对路径的话,如果查找的是子目录的话应该将该本目录路径加到opendir的参数里边,而不是用readdir获取到的目录名。。。。见下面代码。。。
作者: jiayanfu    时间: 2011-08-16 17:35
本帖最后由 jiayanfu 于 2011-08-16 17:42 编辑

采用绝对路径,代码修改结果{:2_168:}

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. #include<dirent.h>
  5. void dirls(DIR *dirp,int num,char *pPath); //修改点1:增加路径参数
  6. int main(int argc,char **argv)
  7. {
  8.         ssize_t readnum;
  9.         DIR *dirp;
  10.         struct dirent *direntp;
  11.         if(argc<2)
  12.         return 0;
  13.         dirp=opendir(argv[1]);
  14.         dirls(dirp,1,argv[1]);
  15.         return 0;
  16. }
  17. void dirls(DIR *dirp,int num,char *pPath)
  18. {
  19.         struct dirent *direntp;
  20.         while(NULL != (direntp=readdir(dirp)))
  21.         {
  22.                 DIR *dir;
  23.                 fprintf(stdout,"%d           %s \n",num,direntp->d_name);
  24.                                //修改点2:增加路径参数
  25.                 char chTempDir[1024];
  26.                 memset(chTempDir,0,sizeof(chTempDir));
  27.                 sprintf(chTempDir,"%s//",pPath);
  28.                 strcat(chTempDir,direntp->d_name);
  29.                 if(0!=strcmp(direntp->d_name,".")&&0!=strcmp(direntp->d_name,"..")&&NULL!=(dir=opendir(chTempDir)))
  30.                 {
  31.                         fprintf(stdout,"\n***********************%s*******************************\n",direntp->d_name);
  32.                         dirls(dir,num+1,chTempDir);
  33.                         fprintf(stdout,"\n#######################%s##################################\n",direntp->d_name);
  34.                 }
  35.         }
  36.         closedir(dirp);
  37. }



复制代码

作者: x6666f    时间: 2011-08-16 18:45
回复 4# jiayanfu


    谢了啊




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2