首页 > 编程语言 > 详细

C++遍历文件及文件夹代码

时间:2016-03-28 15:03:36      阅读:146      评论:0      收藏:0      [点我收藏+]

可以遍历目录包含的文件及文件夹

 1 #include <string>
 2 #include <vector>
 3 #include <io.h>
 4 
 5 using std::vector;
 6 using std::string;
 7 
 8 void getAllFiles(string path, vector<string>& files)
 9 {
10     //文件句柄 
11     long  hFile = 0;
12     //文件信息 
13     struct _finddata_t fileinfo;
14     string p;
15     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
16     {
17         do
18         {
19             if ((fileinfo.attrib & _A_SUBDIR))
20             {
21                 if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
22                 {
23                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name)); // 路径名
24                     getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files); // 递归子目录
25                 }
26             }
27             else
28             {
29                 files.push_back(p.assign(path).append("\\").append(fileinfo.name)); // 完整路径+文件名
30                 //files.push_back(fileinfo.name); // 文件名
31             }
32         } while (_findnext(hFile, &fileinfo) == 0);
33         _findclose(hFile);
34     }
35 }

 

C++遍历文件及文件夹代码

原文:http://www.cnblogs.com/SFMing/p/5328750.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!