因为使用_finddata_t所以需要加#include <io.h>
具体实现代码如下:
#pragma once
#include "pch.h"
#include <stdio.h>
#include <io.h>
#include <string>
int main()
{
//目标文件夹路径
std::string inPath = "E://tb//project//tb5//faced//*.jpg";//遍历文件夹下的所有.jpg文件
//用于查找的句柄
long long handle;
struct _finddata_t fileinfo;
//第一次查找
handle = _findfirst(inPath.c_str(), &fileinfo);
if (handle == -1)
return -1;
do
{
//找到的文件的文件名
printf("%s\n", fileinfo.name);
} while (!_findnext(handle, &fileinfo));
_findclose(handle);
system("pause");
return 0;
}
_findfirst()返回类型为intptr_t而非long型,从“intptr_t”转换到“long”丢失了数据
因此将long hFile; 改为 intptr_t hFile; 即可。
或hFile 须为 long long 类型,因为 _findnext 函数的第一个参数类型是 intptr_t
在写这段代码的过程中,我犯了很让自己困惑的错误,我的错误过程:
我原先在 win10 vs2010 中写过类似的代码,定义 long hFile = 0,运行一直都没有问题,但在 vs2017 中运行就是错误的,在 _findnext 处报异常,我花了大量的时间查看路径和对比代码,发现并没有问题,一直苦思直到心态快爆炸了,于是打开 googl

本文介绍了在VS2017中使用_Cfindfirst与_findnext遍历文件夹图片时遇到的问题。由于*_finddata_t与intptr_t类型的不匹配,导致编译错误。解决方案是将变量hFile类型更改为intptr_t或long long。作者分享了自己的错误经历,提醒开发者在遇到问题时应先定位错误,避免盲目修改和调试。
最低0.47元/天 解锁文章
1232





