-
C++获取指定文件夹下的所有文件名(或路径)
- #include <io.h>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <iostream>
- using namespace std;
-
- //其实两个字符串连在一起比如string可惜写成:str1+str2+“xxx”。。。
- void GetAllFiles( string path, vector<string>& files)
- {
-
- long hFile = 0;
-
- struct _finddata_t fileinfo;
- string p;
- if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) != -1)
- {
- do
- {
- if((fileinfo.attrib & _A_SUBDIR))
- {
- if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
- {
- files.push_back(p.assign(path).append("\").append(fileinfo.name) );
- GetAllFiles( p.assign(path).append("\").append(fileinfo.name), files );
- }
- }
- else
- {
- files.push_back(p.assign(fileinfo.name) );
- }
-
- }while(_findnext(hFile, &fileinfo) == 0);
-
- _findclose(hFile);
- }
-
- }
-
- void GetAllFormatFiles( string path, vector<string>& files,string format)
- {
-
- long hFile = 0;
-
- struct _finddata_t fileinfo;
- string p;
- if((hFile = _findfirst(p.assign(path).append("\*" + format).c_str(),&fileinfo)) != -1)
- {
- do
- {
- if((fileinfo.attrib & _A_SUBDIR))
- {
- if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
- {
-
- GetAllFormatFiles( p.assign(path).append("\").append(fileinfo.name), files,format);
- }
- }
- else
- {
- files.push_back( p.assign(fileinfo.name));
- }
- }while(_findnext(hFile, &fileinfo) == 0);
-
- _findclose(hFile);
- }
- }
-
-
- int main()
- {
- string filePath = "D:\文档\课程\数据结构\MOOC练习\testPCL\x64\Debug";
- vector<string> files;
- char * distAll = "AllFiles.txt";
-
-
-
-
-
- string format = ".dll";
- GetAllFormatFiles(filePath, files,format);
- ofstream ofn(distAll);
- int size = files.size();
- ofn<<size<<endl;
- for (int i = 0;i<size;i++)
- {
- ofn<<files[i]<<endl;
- cout<< files[i] << endl;
- }
- ofn.close();
- return 0;
- }
-
相关阅读:
Codeforces Round #439 (Div. 2) B. The Eternal Immortality
Codeforces Round #439 (Div. 2) A. The Artful Expedient
Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0
ClassLoader
UVA 10790 How Many Points of Intersection?
HDU 4628 Pieces
Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法
thrift之TTransport层的分帧传输类TFramedTransport
VB6基本数据库应用(四):数据的提取,新增和修改
android 开发中判断网络是否连接的代码
-
原文地址:https://www.cnblogs.com/1996313xjf/p/5918456.html
Copyright © 2020-2023
润新知