Linux环境下遍历文件所有内容


#include   <dirent.h>   
#include   <sys/types.h>   
#include   <sys/stat.h>   
#include <unistd.h>

int count = 0;   
void   dir_scan(char   *path,   char   *file)   
{   
                   struct   stat   s;   
                   DIR           *dir;   
                   struct   dirent   *dt;   
                   char   dirname[128];   
                   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);
								    //strcat(dirname,file);

									//cout << dirname << endl;
									//GetLineAndPrint(dirname);
                                   count++;   
                   }   
}

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);   
}   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值