【发布时间】:2016-03-08 20:49:51
【问题描述】:
我在 Linux 中有以下代码来查找与给定通配符匹配的文件:
std::vector<std::string> listFilenamesInPath(std::string wildcard = "*", std::string directory = "./")
{
std::vector<std::string> result;
DIR* dirp = opendir(directory.c_str());
if (dirp)
{
while (true)
{
struct dirent* de = readdir(dirp);
if (de == NULL)
break;
if (fnmatch(wildcard.c_str(), de->d_name, 0))
continue;
else
result.push_back (std::string(de->d_name));
}
closedir(dirp);
}
std::sort(result.begin(), result.end());
return result;
}
我正在将此代码移植到Windows,发现fnmatch不可用(dirent也不可用,但我可以根据以下SO link找到一个。
是否有一个 fnmatch 替代函数可以做完全相同的事情?
如何在不破坏逻辑的情况下让这段代码在 VS2012 中编译和运行?
【问题讨论】:
-
msdn.microsoft.com/en-us/library/windows/desktop/…
FindFirstFile(),FindNextFile()...函数系列可能就是您要找的。span> -
如果 boost 是一个选项,它会提供在 Linux 和 Windows 上运行的类似功能。 boost::filesystem , boost::path 等
标签: c++ windows visual-studio-2010