首页 > 编程语言 > 详细

C++ 获取文件夹下的所有文件名

时间:2015-12-07 11:57:07      阅读:249      评论:0      收藏:0      [点我收藏+]

原文:http://blog.csdn.net/cxf7394373/article/details/7195661

 1 #include<iostream>
 2 #include<fstream>
 3 #include<vector>
 4 #include<string.h>
 5 #include<io.h>
 6 using namespace std;
 7 void getfiles(string filepath, vector<string> &files);
 8 
 9 int main() {
10     fstream file;
11     vector<string> files;
12     string filepath = "D:\\学习资料";
13     getfiles(filepath, files);
14     int size = files.size();
15     for (int i = 0; i < size; i++) {
16         cout << files[i].c_str() << endl;
17     }
18     return 0;
19 }
20 
21 void getfiles(string path, vector<string>& files) {
22     //文件句柄
23     long hFile = 0;
24     //文件信息
25     struct _finddata_t fileinfo;
26     string p;
27     if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo))
28             != -1) {
29         do {
30             //如果是目录,迭代之
31             //如果不是,加入列表
32             if ((fileinfo.attrib & _A_SUBDIR)) {
33                 if (strcmp(fileinfo.name, ".") != 0
34                         && strcmp(fileinfo.name, "..") != 0)
35                     getfiles(p.assign(path).append("\\").append(fileinfo.name),
36                             files);
37             } else {
38                 files.push_back(
39                         p.assign(path).append("\\").append(fileinfo.name));
40             }
41         } while (_findnext(hFile, &fileinfo) == 0);
42         _findclose(hFile);
43     }
44 }

 

C++ 获取文件夹下的所有文件名

原文:http://www.cnblogs.com/sindy/p/5025313.html

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