unix 系统上的 C++ 中的简单 glob?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8401777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:30:34  来源:igfitidea点击:

Simple glob in C++ on unix system?

c++unixglob

提问by Benjamin Crouzier

I want to retrieve all the matching paths following this pattern in a vector<string>:

我想在 a 中检索遵循此模式的所有匹配路径vector<string>

"/some/path/img*.png"

How can I simply do that ?

我怎么能简单地做到这一点?

回答by Piti Ongmongkolkul

I have that in my gist. I created a stl wrapper around glob so that it returns vector of string and take care of freeing glob result. Not exactly very efficient but this code is a little more readable and some would say easier to use.

我的大意就是这样。我在 glob 周围创建了一个 stl 包装器,以便它返回字符串向量并处理释放 glob 结果。不是很有效,但这段代码可读性更强,有些人会说更容易使用。

#include <glob.h> // glob(), globfree()
#include <string.h> // memset()
#include <vector>
#include <stdexcept>
#include <string>
#include <sstream>

std::vector<std::string> glob(const std::string& pattern) {
    using namespace std;

    // 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(), 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;
}

回答by sth

You can use the glob()POSIX library function.

您可以使用glob()POSIX 库函数。

回答by szx

I wrote a simple globlibrary for Windows & Linux (probably works on other *nixes as well) a while ago when I was bored, feel free to use it as you like.

不久前,当我感到无聊时,我为 Windows 和 Linux编写了一个简单的glob库(可能也适用于其他 *nix),请随意使用它。

Example usage:

用法示例:

#include <iostream>
#include "glob.h"

int main(int argc, char **argv) {
  glob::Glob glob(argv[1]);
  while (glob) {
    std::cout << glob.GetFileName() << std::endl;
    glob.Next();
  }
}

回答by mousomer

I've tried the solutions above on Centos6, and I found out that I needed to change:

我已经在 Centos6 上尝试了上面的解决方案,我发现我需要改变:

int ret = glob(pat.c_str(), 0, globerr, &glob_result);

(where "globerr" is an error handling function)

(其中“globerr”是错误处理函数)

Without the explicit 0, I got "GLOB_NOSPACE" error.

如果没有显式 0,我会收到“GLOB_NOSPACE”错误。

回答by rakesh

Mostlikely http://www.boost.org/doc/libs/release/libs/regex/is the closest you will get. There is a good chance this will be supported in C++11.

很可能http://www.boost.org/doc/libs/release/libs/regex/是您最接近的。这很有可能在 C++11 中得到支持。