Linux下的文件遍历和Windows下的文件遍历示例

Linux下的文件遍历和Windows下的文件遍历示例

C算法设计  |  目录遍历算法

1.Linux下的目录遍历函数
 int num=0;
 int i=0;
 int j = 0;
 int k=0;
 int dir_len=0;
    int m=0;
    char temp[MAX_PATH_LEN]; 
 struct dirent **namelist ;
 struct stat statbuf = {0};
 
 char dir[200] = "./";
 char Pattern[200] = "";
 
 if(path == NULL)
  return FALSE;
 num = scandir(path, &namelist, 0, alphasort);
 if(-1 != num)
 {
  /*Get Directory*/
  for(i = 0; i < num; i++)
  {
   if(!strcmp(namelist[i]->d_name,".")||!strcmp(namelist[i]->d_name,".."))       
   {
    continue;
   } 
   sprintf(dir,"%s/%s",path,namelist[i]->d_name);
   if(-1 != stat(dir, &statbuf))
   {
    if(S_ISDIR(statbuf.st_mode))
    {
      对应的目录处理过程
      递归调用自己实现广度遍历  }
 
    }
    else
    {
        对文件的处理过程   

    }

 }
2.Windows下的目录遍历函数 

 
 struct _finddata_t filefind;
 char curr[MAX_PATH_LEN]; 
 char xing[]="*.*";
 int  done = 0,m=0;  
 int  handle;
 int  i = 0;
 
 strcpy(curr,path);
 strcat(curr,"//*.*");
 
 if((-1) ==(handle = _findfirst(curr,&filefind)))
 {
  return FALSE;
 }
 while(!(done = _findnext(handle,&filefind)))
 {
  if(!strcmp(filefind.name,".")||!strcmp(filefind.name,".."))
  {
   continue;
  }
  
  if (_A_SUBDIR & filefind.attrib)
  {    

      对应的目录处理过程
      递归调用自己实现广度遍历  }
  else
  { 
   对文件的处理过程  

   }    
 }
 _findclose(handle);  
#endif

 

需要用到目录遍历的朋友请不要忘了添加头文件<dirent.h>到你的头文件中

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值