windows环境下_findnext()调试出现访问冲突异常。

本文描述了在Windows7 x64环境下,使用VS2013进行编程时遇到的_findnext()函数异常问题。该异常源于文件句柄类型不匹配导致的数据丢失。文章详细介绍了错误现象、代码段及解决方案,即将文件句柄类型由long更改为intptr_t。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    windows7,x64,VS2013环境下,使用_findnext()获取某文件夹内某类型文件名,报异常:

    0x0000000077AEDA56 (ntdll.dll) ...处的第一机会异常:  0xC0000005写入位置 0xFFFFFFFFFFB55F3460时发生访问冲突。如图所示:

    异常代码如下:

void GetFiles(std::string filePath, std::vector<std::string> &filesname, std::string strFileSuffix)
{
	int len = strFileSuffix.length();
	std::string temp, p;

	_finddata_t file; 
	long hf; 
	if ((hf = _findfirst(p.assign(filePath).append("\\*").c_str(), &file)) == -1) {
		std::cout << filePath << " not found files!!!" << std::endl;
	}
	else {
		while (_findnext(hf, &file) == 0) { 
			if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
				continue;
			temp = file.name;
			if (temp.length()>len && temp.compare(temp.length() - len, len, strFileSuffix) == 0)
				filesname.push_back(file.name);
		}
	}
	_findclose(hf);

	return;
}

    最终确定问题所在:_findnext()函数返回值是intptr_t类型,而文件句柄hf类型为long,从intptr_t转换到long丢失了数据。

    解决方法:将文件句柄类型改为intptr_t。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值