Ask Your Question

Revision history [back]

I found this c++ implementation of the glob function some time ago and have been using it since:

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; }

You can call this function as:

filenames = glob("<path>/*.<extension>");

I found this c++ implementation of the glob function some time ago and have been using it since:

std::vector<std::string> glob(const glob(const std::string& pattern) { using
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; }

You can call this function as:

filenames = glob("<path>/*.<extension>");