windows7,x64,VS2013环境下,使用_findnext()获取某文件夹内某类型文件名,报异常:
0x0000000077AEDA56 (ntdll
.dll) ...
处的第一机会异常:
0xC0000005:
写入位置
0xFFFFFFFFFFB55F3460时发生访问冲突。如图所示:
异常代码如下:
void GetFiles(std::string filePath, std::vector<std::string> &filesname, std::string strFileSuffix)
{
int len = strFileSuffix.length();
std::string temp, p;
_finddata_t file;
long hf;
if ((hf = _findfirst(p.assign(filePath).append("\\*").c_str(), &file)) == -1) {
std::cout << filePath << " not found files!!!" << std::endl;
}
else {
while (_findnext(hf, &file) == 0) {
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
continue;
temp = file.name;
if (temp.length()>len && temp.compare(temp.length() - len, len, strFileSuffix) == 0)
filesname.push_back(file.name);
}
}
_findclose(hf);
return;
}
最终确定问题所在:_findnext()函数返回值是intptr_t类型,而文件句柄hf类型为long,从intptr_t转换到long丢失了数据。
解决方法:将文件句柄类型改为intptr_t。