有人知道可以等效于 python glob 函数的仅 c/cpp 包吗?
基本上,我正在寻找这样的东西:
string startDirectory = "c:\foo\bar\*.txt"
vector<string> filename_list = getFilenameList(startDirectory)
注意:Python 有一种非常可爱的方式来做到这一点:
glob(startDirectory)
注意:正在寻找一些在 Windows 和 Linux 中有效但没有提升 的实现 - 只是标准 c++,c.
这是具有矛盾要求的问题之一:
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
您可能没有意识到可移植代码到底有多痛苦。例如,如果您的用户将 Unicode 字符放入其文件名中,则 std::ifstream
在 Windows 上基本上会被破坏。这就是我们热爱图书馆的原因。
注意事项
C 或 C++ 标准库中没有允许您列出目录内容的功能。如果您想进行 globbing,您必须使用特定于平台的代码。在这个问题上,您唯一的选择是您自己编写充满错误的代码,还是像其他人一样使用经过良好测试的库。