C++中_findnext()函数触发断点问题

今晚遇到了个使用c++遍历文件夹中所有文件的问题,参考了此篇博客,初次了解到 _finddata_t 结构体的用法,但是在使用参考程序的时候遇到了下面的错误

点到_findnext函数中发现,第一个参数类型是intptr_t类型的,再转到intptr_t的定义,发现intptr_t是 __int64的另一种声明,那么我们就知道这里出现的错误了,之前程序中的第一个参数的类型是long类型的,long类型的数据范围是-2^31~2^31-1,但是__int64的数据范围是-2^63~ 2^63-1,那么由long转换到__int64一定会有精度上的损失,所以这里将handle的数据类型转换为intptr_t或者__int64都是可以的

#include <opencv2/core/utility.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/features2d.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2\opencv.hpp"

#include<iostream>
#include<io.h>
using namespace std;

const char *to_search = "E:\\chrome download\\*";

int main()
{
	intptr_t handle;//用于查找句柄
	struct _finddata_t fileinfo;//文件信息的结构体
	handle = _findfirst(to_search, &fileinfo);//第一次查找
	if (handle == -1)
		return -1;
	printf("%s\n", fileinfo.name);//打印出第一个文件名
	while (!_findnext(handle, &fileinfo))
	{
		printf("%s\n", fileinfo.name);
	}
	_findclose(handle);//别忘了关闭句柄

	system("pause");
	return 0;
}

参考博客

https://blog.csdn.net/guanyuqiu/article/details/79726271

后记

     暑假到现在一直在做一个关于Opencv的项目,所以没怎么写算法的博客,打算从今天开始,会写一些关于项目中遇到的问题以及一些数据结构等内容,算法内容等项目完成后希望可以慢慢地跟上。

    十点的学院楼我胡汉三又回来了!

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/82972504