VS2013遍歷目錄下所有文件時產生中斷錯誤

void getFiles(char* path, vector<string>& files)
{
	string p = path;
	size_t f = p.find("*");
	string p1 = p.substr(0, f);


	long handle;    

	struct _finddata_t fileinfo;
	handle = _findfirst(path, &fileinfo);
	if (-1 == handle)return;

	files.push_back(p1.append(fileinfo.name));

	while (!_findnext(handle, &fileinfo))
	{
		files.push_back(p1.append(fileinfo.name));
	}

	_findclose(handle);
}

原因:_findfirst()返回類型是intptr_t而不是long,從intptr_t轉換爲long的過程中丟失了數據。

解決:將handle的類型修改爲intptr_t即可。

void getFiles(char* path, vector<string>& files)
{
	string p = path;
	size_t f = p.find("*");
	string p1 = p.substr(0, f);


	//long handle;
	intptr_t handle = 0;

	struct _finddata_t fileinfo;
	handle = _findfirst(path, &fileinfo);
	if (-1 == handle)return;

	files.push_back(p1.append(fileinfo.name));

	while (!_findnext(handle, &fileinfo))
	{
		files.push_back(p1.append(fileinfo.name));
	}

	_findclose(handle);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章