using namespace std;
#include <string>
#include <dirent.h>
#include <iostream>
#include <vector>
int FindFileList(const char * path,string & wildcard,vector<string> &result){
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(path) ) ==NULL){
cout << "open dir failed " + string(path) << endl;
return 1;
}
while ( (ptr = readdir(dir)) !=NULL){
if (string(ptr->d_name).find(wildcard) != string::npos){
result.push_back(string(ptr->d_name));
cout << "\t\t\t\tfind file " + string(ptr->d_name) << endl;
}
}
return 0;
}
int main()
{
string path = ".";
string wildcard = "cpp";
vector<string> vct;
FindFileList(path.c_str(),wildcard,vct);
vector<string>::iterator it = vct.begin();
for(; it != vct.end() ; it++){
cout << "===================================" + *it + "===============" << endl;
}
return 0;
}
运行结果: