VS2013遍历目录下所有文件时产生中断错误
void getFiles(char* path, vector<string>& files)
{
string p = path;
size_t f = p.find("*");
string p1 = p.substr(0, f);
long handle;
struct _finddata_t fileinfo;
handle = _findfirst(path, &fileinfo);
if (-1 == handle)return;
files.push_back(p1.append(fileinfo.name));
while (!_findnext(handle, &fileinfo))
{
files.push_back(p1.append(fileinfo.name));
}
_findclose(handle);
}
原因:_findfirst()返回类型是intptr_t而不是long,从intptr_t转换为long的过程中丢失了数据。
解决:将handle的类型修改为intptr_t即可。
void getFiles(char* path, vector<string>& files)
{
string p = path;
size_t f = p.find("*");
string p1 = p.substr(0, f);
//long handle;
intptr_t handle = 0;
struct _finddata_t fileinfo;
handle = _findfirst(path, &fileinfo);
if (-1 == handle)return;
files.push_back(p1.append(fileinfo.name));
while (!_findnext(handle, &fileinfo))
{
files.push_back(p1.append(fileinfo.name));
}
_findclose(handle);
}