方法一
使用_findfirst和_findnext
#include <io.h>
intptr_t hFile = -1;
struct _finddata_t file;
std::string pathName = "D:\\";
if (hFile = _findfirst(pathName.append("\\*.*").c_str(), &file) != -1)
{
do
{
std::cout << file.name << ", " << file.size << "bytes\n";
} while (_findnext(hFile, &file) == 0);
}
_findclose(hFile);
本人使用该方法无法遍历,hFile值为1,不明就里
方法二
WIN api
#include <windows.h>
std::string path = "D:\\";
WIN32_FIND_DATAA fileInfo;
HANDLE hFile = FindFirstFileA((path + "*.*").c_str(), &fileInfo);
if (hFile == INVALID_HANDLE_VALUE) {
return;
}
do
{
if (strstr(fileInfo.cFileName, ".bmp"))
{
filePath.push_back(path + fileInfo.cFileName);
}
} while (FindNextFileA(hFile, &fileInfo));
不得不说winapi大法好