C++ 根據通配符查找目錄下文件

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;

     
}


運行結果:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章