在unix系统上的C ++中的简单glob? [英] Simple glob in C++ on unix system?
本文介绍了在unix系统上的C ++中的简单glob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要检索向量< string>
中此模式后的所有匹配路径:
I want to retrieve all the matching paths following this pattern in a vector<string>
:
"/some/path/img*.png"
我只是这样做?
推荐答案
我有我的要求。我在glob周围创建了一个stl包装,以便它返回字符串的向量并且注意释放glob结果。不是非常有效,但让你的代码看起来更容易阅读。
I have that in my gist. I created a stl wrapper around glob so that it return vector of string and take care of freeing glob result. Not exactly very efficient but make your code looks more readable.
#include <glob.h>
#include <vector>
#include <string>
inline std::vector<std::string> glob(const std::string& pat){
using namespace std;
glob_t glob_result;
glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
vector<string> ret;
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
ret.push_back(string(glob_result.gl_pathv[i]));
}
globfree(&glob_result);
return ret;
}
这篇关于在unix系统上的C ++中的简单glob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文