C++读取文件夹下的所有文件

读取文件夹下的所有文件名,并将文件名存放到容器中。 

//获取文件夹下的所有文件的文件名,并存放到vector中
std::vector<std::string> getfile(const std::string &path) {
	std::vector<std::string> vPath;
	intptr_t handle;
	struct _finddata_t fileinfo;
	handle = _findfirst(path.c_str(), &fileinfo);
	if (handle == -1) {
		return vPath;
	}
	do {
		vPath.push_back(fileinfo.name);
	} while (!_findnext(handle, &fileinfo));

	_findclose(handle);
	return vPath;
}

得到的容器如果为空,则说明没有读取到文件。

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