使用findfirst findnext遍历文件时,发生访问冲突错误 错误定位到ntdll.dll
代码为:
void getFiles(string path, vector<string>& files)//lxk20160302
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
cout << path.c_str()<<endl;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
_findnext(hFile, &fileinfo);
do
{
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR))
{
cout << fileinfo.name << endl;
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
continue;
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
http://blog.youkuaiyun.com/cdownload_zxl/article/details/51853524
_findfirst()返回类型为intptr_t而非long型,从“intptr_t”转换到“long”丢失了数据
因此将
long hFile; 改为 intptr_t hFile; 即可。