加入收藏 | 设为首页 | 会员中心 | RSS
您当前的位置:首页 > Linux技术 > 编程开发

linux下c语言递归遍历文件目录

时间:2008-06-06 10:56:56  来源:  作者:
#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++;  
                  }  
  }
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
    无相关信息
栏目更新
栏目热门