在win10中,使用文件遍歷函數_findnext會報0xC0000005錯誤
原因:
_findnext()第一個參數”路徑句柄”,返回的類型爲intptr_t(long long),如果定義爲long,在win7中是沒有問題,但是在win10中就要改爲long long或者intptr_t
下面是示例代碼:
/* Get all files in a folder specified by path and store the file names in a vector */
void getAllFiles(string path, vector<string> &files) {
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}