解决Win10下_findnext()异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mao0514/article/details/87066731

在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);
    }

}


 

猜你喜欢

转载自blog.csdn.net/mao0514/article/details/87066731