免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2116 | 回复: 4
打印 上一主题 下一主题

我的linux代码不能遍历所有目录,求助大神们 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-16 16:03 |只看该作者 |倒序浏览
我的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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-08-16 16:10 |只看该作者
建议用递归实现!~~~

论坛徽章:
0
3 [报告]
发表于 2011-08-16 17:11 |只看该作者
本帖最后由 jiayanfu 于 2011-08-16 17:46 编辑

原因在于楼主对于opendir的参数不对,打开上级或者下级的目录的时候,那个路径给的不对。采用相对路径的话,可能稍微复杂点。。。如果采用绝对路径的话,如果查找的是子目录的话应该将该本目录路径加到opendir的参数里边,而不是用readdir获取到的目录名。。。。见下面代码。。。

论坛徽章:
0
4 [报告]
发表于 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. }



复制代码

论坛徽章:
0
5 [报告]
发表于 2011-08-16 18:45 |只看该作者
回复 4# jiayanfu


    谢了啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP