[捉虫记录] access violation writing location _findnext

jeremy lin

在Windows 10中VS2013下运行如下程序:

void getFiles(string path, vector<string>& files)
{
	long hFile = 0;

	// _finddata_t ÊÇÓÃÀ´´æ´¢Îļþ¸÷ÖÖÐÅÏ¢µÄ½á¹¹Ìå
	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));
					getFiles(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);
	}

}

void getFilesFormat(string path, vector<string>& files, string format)
{

	//long   hFile = 0;
	intptr_t hFile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
					getFilesFormat(p.assign(path).append("\\").append(fileinfo.name), files, format);
				}
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile);
	}
}

报如下错误:

access violation writing location

[捉虫记录] access violation writing location _findnext_第1张图片


note:在win10 vs2015情况下运行程序未报错。


解决方案:

把long hFile改成 intptr_t hFile


你可能感兴趣的:(bug)