C++ 获取具有特定模式c++;linux与windows
我正试图从特定目录中获取具有特定模式的所有文件。C++ 获取具有特定模式c++;linux与windows,c++,linux,windows,cross-platform,C++,Linux,Windows,Cross Platform,我正试图从特定目录中获取具有特定模式的所有文件。 例如“c:\temp”中的“ia\u aiq\u usecase.*.bin” 我的windows代码运行良好。 但是在linux中,我不想使用boost。而且我似乎找不到一种方法来获取具有特定模式的所有文件 你能帮忙吗 有没有跨平台的方法 const std::vector<std::string> OS::GetAllFiles(std::string directoryPath, std::string filePattern)
例如“c:\temp”中的“ia\u aiq\u usecase.*.bin” 我的windows代码运行良好。 但是在linux中,我不想使用boost。而且我似乎找不到一种方法来获取具有特定模式的所有文件
你能帮忙吗 有没有跨平台的方法
const std::vector<std::string> OS::GetAllFiles(std::string directoryPath, std::string filePattern)
{
#ifdef _WIN32
std::vector<std::string> files;
std::string pathAndPattern = directoryPath + OS::PathSeperator() + filePattern;
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(ToWString(pathAndPattern).c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
files.push_back(FromWString(fd.cFileName));
}
}
while (::FindNextFile(hFind, &fd));
{
::FindClose(hFind);
}
}
return files;
#else
std::vector<std::string> files;
std::string search_path = directoryPath + OS::PathSeperator() + filePattern;
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(directoryPath.c_str())) == NULL)
{
std::cout << "Error(" << errno << ") opening " << directoryPath << std::endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
#endif
}
const std::vector OS::GetAllFiles(std::string directoryPath,std::string filePattern)
{
#ifdef_WIN32
std::矢量文件;
std::string pathAndPattern=directoryPath+OS::PathSeperator()+filePattern;
WIN32_FIND_DATA fd;
HANDLE hFind=::FindFirstFile(ToWString(pathAndPattern).c_str(),&fd);
if(hFind!=无效的句柄值)
{
做
{
if(!(fd.dwFileAttributes和文件属性目录))
{
push_back(FromWString(fd.cFileName));
}
}
while(::FindNextFile(hFind,&fd));
{
::FindClose(hFind);
}
}
归还文件;
#否则
std::矢量文件;
std::string search_path=directoryPath+OS::PathSeperator()+filePattern;
DIR*dp;
结构方向*dirp;
if((dp=opendir(directoryPath.c_str())==NULL)
{
STD::你使用C++版本什么?C++ 14对文件系统有实验支持,它包含在即将到来的C++ 17标准中。还有<代码> Boo::FLASSULTS/COD>这是一个复制品,它有很多创造性的解决方案。@ NANOLIVER GCC 4.8。1@UlfLunde我需要一个文件模式。给定目录中所有文件的列表,y您可以自己将它们与模式匹配。(我不是说没有跨平台的方法来获取模式匹配的文件列表,我不知道是否有。我只是说您不需要该功能。)C++ C++ 14对文件系统有实验支持,它包含在即将到来的C++ 17标准中。还有<代码> Boo::FLASSULTS/COD>这是一个复制品,它有很多创造性的解决方案。@ NANOLIVER GCC 4.8。1@UlfLunde我需要一个文件模式。给定目录中所有文件的列表,您可以我不是说没有跨平台的方法来获取模式匹配的文件列表,我不知道是否有。我只是说你不需要这个功能