如何使用 C++ 98 在 linux 中匹配文件名模式
How to glob file name pattern matching in linux using c++ 98
下面是一段代码,我试图从提供的目录路径中找到具有匹配模式的文件。
预期是列出所有具有模式匹配的文件,
例如在 "/usr/local" 路径下,存在以下文件
abc.txt axy.txt bcd.txt azz.txt bby.txt
使用模式匹配代码,我期待以下输出
abc.txt
axy.txt
azz.txt
#include <glob.h>
#include <string.h>
#include <vector>
#include <stdexcept>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
vector<string> glob(const string& pattern) {
// glob struct resides on the stack
glob_t glob_result;
memset(&glob_result, 0, sizeof(glob_result));
// do the glob operation
//int return_value = glob(pattern.c_str(), 0, globerr, &glob_result);
int return_value = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
if(return_value != 0) {
globfree(&glob_result);
stringstream ss;
ss << "glob() failed with return_value " << return_value << endl;
throw std::runtime_error(ss.str());
}
// collect all the filenames into a std::list<std::string>
vector<string> filenames;
for(size_t i = 0; i < glob_result.gl_pathc; ++i) {
filenames.push_back(string(glob_result.gl_pathv[i]));
}
// cleanup
globfree(&glob_result);
// done
return filenames;
}
int main(int argc, char **argv) {
vector<string> res;
res= glob("a");
return 0;
}
如评论中所述,您将只匹配一个完全命名为 a
的文件,您 globfree
在不应该的时候抛出一个您没有捕获到的异常。
#include <glob.h>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
std::vector<std::string> glob(const std::string& pattern) {
glob_t glob_result = {0}; // zero initialize
// do the glob operation
int return_value = ::glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
if(return_value != 0) throw std::runtime_error(std::strerror(errno));
// collect all the filenames into a std::vector<std::string>
// using the vector constructor that takes two iterators
std::vector<std::string> filenames(
glob_result.gl_pathv, glob_result.gl_pathv + glob_result.gl_pathc);
// cleanup
globfree(&glob_result);
// done
return filenames;
}
int main() {
try { // catch exceptions
std::vector<std::string> res = glob("*a*"); // files with an "a" in the filename
for(size_t i = 0; i< res.size(); ++i)
std::cout << res[i] << '\n';
} catch(const std::exception& ex) {
std::cerr << ex.what() << std::endl;
}
}
下面是一段代码,我试图从提供的目录路径中找到具有匹配模式的文件。
预期是列出所有具有模式匹配的文件, 例如在 "/usr/local" 路径下,存在以下文件 abc.txt axy.txt bcd.txt azz.txt bby.txt
使用模式匹配代码,我期待以下输出
abc.txt
axy.txt
azz.txt
#include <glob.h>
#include <string.h>
#include <vector>
#include <stdexcept>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
vector<string> glob(const string& pattern) {
// glob struct resides on the stack
glob_t glob_result;
memset(&glob_result, 0, sizeof(glob_result));
// do the glob operation
//int return_value = glob(pattern.c_str(), 0, globerr, &glob_result);
int return_value = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
if(return_value != 0) {
globfree(&glob_result);
stringstream ss;
ss << "glob() failed with return_value " << return_value << endl;
throw std::runtime_error(ss.str());
}
// collect all the filenames into a std::list<std::string>
vector<string> filenames;
for(size_t i = 0; i < glob_result.gl_pathc; ++i) {
filenames.push_back(string(glob_result.gl_pathv[i]));
}
// cleanup
globfree(&glob_result);
// done
return filenames;
}
int main(int argc, char **argv) {
vector<string> res;
res= glob("a");
return 0;
}
如评论中所述,您将只匹配一个完全命名为 a
的文件,您 globfree
在不应该的时候抛出一个您没有捕获到的异常。
#include <glob.h>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
std::vector<std::string> glob(const std::string& pattern) {
glob_t glob_result = {0}; // zero initialize
// do the glob operation
int return_value = ::glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
if(return_value != 0) throw std::runtime_error(std::strerror(errno));
// collect all the filenames into a std::vector<std::string>
// using the vector constructor that takes two iterators
std::vector<std::string> filenames(
glob_result.gl_pathv, glob_result.gl_pathv + glob_result.gl_pathc);
// cleanup
globfree(&glob_result);
// done
return filenames;
}
int main() {
try { // catch exceptions
std::vector<std::string> res = glob("*a*"); // files with an "a" in the filename
for(size_t i = 0; i< res.size(); ++i)
std::cout << res[i] << '\n';
} catch(const std::exception& ex) {
std::cerr << ex.what() << std::endl;
}
}