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

原文: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 }

原文链接: https://www.cnblogs.com/sindy/p/5025313.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/225506

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:49
下一篇 2023年2月13日 下午12:50

相关推荐