linux的c下递归访问所有文件

网上看的,转载过来,应该是linux下c语言的环境。该片段可以借鉴作其他用途

代码如下:

  1. #include <dirent.h>  
  2. #include <iostream>  
  3. #include <cstdlib>  
  4. #include <cstring>  
  5. using namespace std;  
  6. void GetFileInDir(string dirName)  
  7. {  
  8.     DIR* Dir = NULL;  
  9.     struct dirent* file = NULL;  
  10.     if (dirName[dirName.size()-1] != '/')  
  11.     {  
  12.         dirName += "/";  
  13.     }  
  14.     if ((Dir = opendir(dirName.c_str())) == NULL)  
  15.     {  
  16.         cerr << "Can't open Directory" << endl;  
  17.         exit(1);  
  18.     }  
  19.     while (file = readdir(Dir))  
  20.     {  
  21.         //if the file is a normal file  
  22.         if (file->d_type == DT_REG)  
  23.         {  
  24.             cout << dirName + file->d_name << endl;  
  25.         }  
  26.         //if the file is a directory  
  27.         else if (file->d_type == DT_DIR && strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)  
  28.         {  
  29.             GetFileInDir(dirName + file->d_name);  
  30.         }  
  31.     }  
  32. }  
  33. int main(int argc, char* argv[])  
  34. {  
  35.     if (argc < 2)  
  36.     {  
  37.         cerr << "Need Directory" << endl;  
  38.         exit(1);  
  39.     }  
  40.     string dir = argv[1];  
  41.     GetFileInDir(dir);  
  42. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值