C++遍历路径下文件的两种方法

我想问一下CSDN,我的标题含有    (_findfirst和FindFirstFile())   咋就是非法字符了?????????

_findfirst方法貌似有的时候不好使,后面研究一下。

void getFilesPath(const string& path, vector<string>& vecFiles)
{
	HANDLE file;
	string p;
	LPCTSTR lpFileName= p.assign(path).append("\\*.txt").c_str();
	WIN32_FIND_DATA pNextInfo;
	file=FindFirstFile(lpFileName, &pNextInfo);
	if (file == INVALID_HANDLE_VALUE)
	{
		return;
	}
	while (FindNextFile(file, &pNextInfo))
	{
		string file = pNextInfo.cFileName;
		string fullpath = path + "\\" + file;
		vecFiles.push_back(fullpath);
	}
}


void getFilesPath(const string& path, vector<string>& vecFiles)
{
	long   hFile = 0;
	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)
				{
					getFilesPath(p.assign(path).append("\\").append(fileinfo.name), vecFiles);
				}
			}
			else
			{
				vecFiles.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}



 

おすすめ

転載: blog.csdn.net/lxiao428/article/details/102837630