网上找了好多代码都不好用,以下代码实测通过,可以读取当前目录下文件名,未对子目录进行搜索,为unicode编码方式。
#include <iostream>
using namespace std;
#include <vector>
//#include <windows.h>
#include <afx.h>
void GetFilesInDir(vector<wstring> &ofiles, wstring DirPath, wstring PostFix);
int main()
{
// file name in the dir
vector<wstring> ofiles;
// dir path and file post fix
wstring DirPath = L"G:\\";
wstring PostFix = L"*.tif";
GetFilesInDir(ofiles, DirPath, PostFix);
for (int i = 0; i < ofiles.size(); i++)
{
wprintf(L"%s\n", ofiles[i].c_str());
}
}
void GetFilesInDir(vector<wstring> &ofiles, wstring DirPath, wstring PostFix)
{
HANDLE hFind;
WIN32_FIND_DATA data;
ofiles.clear();
wstring SearchName = DirPath + PostFix;
hFind = FindFirstFile(SearchName.c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// add the path and file name together
ofiles.push_back(DirPath + data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}