因为使用_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,运行一直都没有