1. 程式人生 > >c++ 遍歷資料夾下的所有檔案

c++ 遍歷資料夾下的所有檔案

同樣的程式碼在VS2010中可以得到正常結果,但是VS2013中卻不行,實在想不通..

沒有找到解決VS2013中遍歷問題的解決辦法,只好另外想辦法.然後想起C++中應該也有能夠實現資料夾遍歷的方法.

在VS2013中實踐一下:

#include <stdio.h>
#include <io.h>
#include <string>

int main()
{
	//目標資料夾路徑
	std::string inPath = "E:\\RuiJie\\VedioCapture\\2018-08-28 11-06-35\\*.jpg";//遍歷資料夾下的所有.jpg檔案
	//用於查詢的控制代碼
	long handle;
	struct _finddata_t fileinfo;
	//第一次查詢
	handle = _findfirst(inPath.c_str(), &fileinfo);
	if (handle == -1)
		return -1;
	do
	{
		//找到的檔案的檔名
		printf("%s\n", fileinfo.name);

	} while (!_findnext(handle, &fileinfo));

	_findclose(handle);
	system("pause");
	return 0;
}

可以遍歷成功

參考部落格: