- #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;
- }
posted @
2016-09-29 00:16
天下岂有长生不灭者
阅读(
2052)
评论()
编辑
收藏
举报