CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Linux/Unix社区 >  程序开发区

在linux下如何实现文件夹的遍历?

楼主andy_cai()2005-12-21 16:18:50 在 Linux/Unix社区 / 程序开发区 提问

需要什么函数呢?  
  问题点数:50、回复次数:6Top

1 楼xsq618()回复于 2005-12-21 16:35:03 得分 9

试试下面的代码,我是在redhat上编译运行的。  
   
  //g++   -o   read_dir   read_dir.cpp  
  //用于列出参数目录下的文件  
   
  #include   <stdio.h>  
  #include   <stdlib.h>  
   
  #include   <sys/types.h>  
  #include   <dirent.h>  
   
  int   main(   int   argc,   char   *   argv[])  
  {  
  DIR *dp;  
  struct   dirent   *dirp;  
   
  if(   argc   !=   2)  
  {  
  printf("not   enough   arguments!   exit!\n");  
  exit(0);  
  }  
   
  if(   (dp   =   opendir(argv[1]))   ==   NULL)  
  {  
  printf("can't   open   %s!\n",argv[1]);  
  exit(0);  
  }  
   
  while(   (dirp   =   readdir(dp))   !=   NULL)  
  printf("%s\n",dirp->d_name);  
   
  closedir(dp);  
  }  
  Top

2 楼sinall()回复于 2005-12-21 17:21:05 得分 9

呵呵《Unix环境高级编程》第一个例子。  
  一下仅实现了列表一个路径下的文件夹,没有遍历部分。  
   
  #include   <sys/types.h>  
  #include   <dirent.h>  
  #include   <stdio.h>  
  #include   <sys/stat.h>  
  //#define   __ourhdr_h  
  //#include   "ourhdr.h"  
   
  int   main(int   argc,char*   argv[])  
  {  
          DIR   *dp;  
          struct   dirent   *   dirp;  
          if   (argc!=2)  
          {  
  //             err_quit("a   single   argument   (the   directory   name)   is   required");  
                  printf("a   single   argument   is   required");  
                  return   0;  
          }  
          if   ((dp=opendir(argv[1]))==NULL)  
          {  
  //             err_sys("cant   open   %s",argv[1]);  
                  printf("cant   open   %s",argv[1]);  
                  return   0;  
          }  
          while((dirp=readdir(dp))!=NULL)  
          {  
                  struct   stat   buf;  
                  if   (lstat(dirp->d_name,   &buf)   <   0)  
                  {  
                          printf("lstat   error");  
                  }  
                  if   (S_ISDIR(buf.st_mode))  
                  {  
                          printf("%s\n",dirp->d_name);  
                  }  
          }  
   
          closedir(dp);  
  //     exit(0);  
  }Top

3 楼qfeng_zhao(鱼儿鱼儿满天飞)回复于 2005-12-21 18:01:38 得分 30

#include   <stdio.h>  
  #include   <dirent.h>  
  #include   <sys/types.h>  
  #include   <sys/stat.h>  
   
  void   dir_scan(char   *path,   char   *file);  
  int   count   =   0;  
   
  int   main(int   argc,   char   *argv[])  
  {  
                  struct   stat   s;  
   
                  if(argc   !=   2){  
                                  printf("one   direction   requried\n");  
                                  exit(1);  
                  }  
                  if(lstat(argv[1],   &s)   <   0){  
                                  printf("lstat   error\n");  
                                  exit(2);  
                  }  
                  if(!S_ISDIR(s.st_mode)){  
                                  printf("%s   is   not   a   direction   name\n",   argv[1]);  
                                  exit(3);  
                  }  
   
                  dir_scan("",   argv[1]);  
   
                  printf("total:   %d   files\n",   count);  
   
                  exit(0);  
  }  
   
  void   dir_scan(char   *path,   char   *file)  
  {  
                  struct   stat   s;  
                  DIR           *dir;  
                  struct   dirent   *dt;  
                  char   dirname[50];  
   
                  memset(dirname,   0,   50*sizeof(char));  
                  strcpy(dirname,   path);  
   
                  if(lstat(file,   &s)   <   0){  
                                  printf("lstat   error\n");  
                  }  
   
                  if(S_ISDIR(s.st_mode)){  
                                  strcpy(dirname+strlen(dirname),   file);  
                                  strcpy(dirname+strlen(dirname),   "/");  
                                  if((dir   =   opendir(file))   ==   NULL){  
                                                  printf("opendir   %s/%s   error\n");  
                                                  exit(4);  
                                  }  
                                  if(chdir(file)   <   0)   {  
                                                  printf("chdir   error\n");  
                                                  exit(5);  
                                  }  
                                  while((dt   =   readdir(dir))   !=   NULL){  
                                                  if(dt->d_name[0]   ==   '.'){  
                                                                  continue;  
                                                  }  
   
                                                  dir_scan(dirname,   dt->d_name);  
                                  }  
                                  if(chdir("..")   <   0){  
                                                  printf("chdir   error\n");  
                                                  exit(6);  
                                  }  
                  }else{  
                                  printf("%s%s\n",   dirname,   file);  
                                  count++;  
                  }  
  }  
  前些天学习时刚写的代码...Top

4 楼tb01412(tb)回复于 2005-12-21 18:19:05 得分 2

呵,用递归不就行了吗?Top

5 楼khyang(天佑)回复于 2005-12-21 21:08:40 得分 0

呵呵..对头Top

6 楼fxismonk(Monk)回复于 2006-03-30 11:01:37 得分 0

谢谢qfeng_zhao(鱼儿鱼儿满天飞)的代码:)  
  不过里面opendir之后没有closedir,提醒一下后来人:)Top

相关问题

  • 如何在linux下用c/c++边编程实现文件夹下所有子目录文件的遍历?
  • 遍历一个文件夹想用空格来区分子文件夹和子文件怎样实现??
  • 文件夹和文件的遍历
  • 关于文件夹遍历的问题
  • 遍历目录,我现在要遍立一个目录,直到里面的所有文件夹,如何实现?
  • 在C++中用什么函数可以实现文件夹的遍历?格式如何?有重赏!
  • 我想利用ISellFolder实现从桌面到其子文件夹和文件的遍历,应该如何做?
  • 如何遍历某个文件夹下的所有文件和文件夹?
  • 如何遍历在指定文件夹及其子文件夹所有文件
  • 请问如何在JAVA中遍历一个文件夹及这个文件夹下的子文件夹

关键词

  • null
  • dirp
  • dp
  • argv
  • argc
  • dir
  • exit
  • printf
  • argument
  • open%

得分解答快速导航

  • 帖主:andy_cai
  • xsq618
  • sinall
  • qfeng_zhao
  • tb01412

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo