win10遍历文件夹

代码参考:
https://blog.csdn.net/lhanchao/article/details/53576311

#include <io.h>//所需头文件
#include <iostream>
#include <string>

using namespace std;

void getAllFileNames(const string& folder_path)
{
	_finddata_t file;
	long long flag;
	string filename = folder_path + "\\*.jpg";//遍历制定文件夹内的jpg文件
	if ((flag = _findfirst(filename.c_str(), &file)) == -1)//目录内找不到文件
	{
		cout << "There is no such type file" << endl;
	}
	else
	{
		//通过前面的_findfirst找到第一个文件
		string name = folder_path + "\\" + file.name;//file.name存放的是遍历得到的文件名

		cout << name << endl;
		//依次寻找以后的文件
		
		while (_findnext(flag, &file) == 0)
		{
			//cout << "dfd" << endl;
			string name = string(folder_path + "\\" + string(file.name));
			cout << name << endl;
		}
	}
	_findclose(flag);
}

int main()
{
	getAllFileNames("D:\\C++\\Project1\\Project1\\test");//test是制定的目录
	getchar();
}

Debug参考
https://blog.csdn.net/hemmingway/article/details/73716980
注意句柄的类型为long long

猜你喜欢

转载自blog.csdn.net/xnmc2014/article/details/88648119