在c/c ++中使用plataform独立的glob函数

The*_*heo 3 c++

有人知道ac/cpp只有一个可以与python glob函数等效的包吗?

基本上,我正在寻找这样的东西:

string startDirectory = "c:\foo\bar\*.txt"
vector<string> filename_list = getFilenameList(startDirectory)
Run Code Online (Sandbox Code Playgroud)

注意:Python有一种非常可爱的方式:

glob(startDirectory)
Run Code Online (Sandbox Code Playgroud)

注意:寻找一些在Windows和Linux中有效的实现,并且没有任何提升 - 只是标准的c ++,c.

Die*_*Epp 6

这是具有相互矛盾要求的问题之一:

  • 没有图书馆

  • 手提

诸如Boost之类的库的主要功能之一是允许您编写可移植代码.你的另一个选择是编写一堆这样的代码:

#if defined _WIN32
#include <Windows.h>
std::vector<std::string> glob(const std::string &pattern)
{

}
#else
#include <glob.h>
std::vector<std::string> glob(const std::string &pattern)
{

}
#endif
Run Code Online (Sandbox Code Playgroud)

您可能没有意识到可移植代码到底有多痛苦.例如,std::ifstream如果用户在其文件名中放入Unicode字符,则在Windows上基本上会被破坏.这就是我们喜欢图书馆的原因.

注意

没有在C或C++标准库,让你列出一个目录的内容特征.如果要进行通配,必须使用特定于平台的代码.你唯一的选择是你是否编写自己的错误代码或者像其他人一样使用经过良好测试的库.