다음 샘플 코드는 다음과 같습니다
#include <fnmatch.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
vector<string> WildcardSearch(const char* pattern,const char* filePath)
{
vector<string> res;
struct dirent *entry;
DIR *dir;
int ret;
dir = opendir(filePath);
if(dir != NULL)
{
while((entry = readdir(dir)) != NULL)
{
ret = fnmatch(pattern,entry->d_name,FNM_PATHNAME | FNM_PERIOD);
if(ret == 0)
{
res.push_back(entry->d_name);
}
else
{
continue;
}
}
}
closedir(dir);
return res;
}
int main(int argc,char*argv[])
{
vector<string> tmp;
char* pattern = argv[1];
char* path = argv[2];
tmp = WildcardSearch(pattern,path);
for(int i=0;i<tmp.size();i++)
cout<<tmp[i]<<endl;
return 0;
}
예를 들면 :
./test "2018-*.bmp" ./path