C++读取某个文件夹下面的子文件夹及其所有文件

一.getAllFiles乱序获取

参考他人博客:https://blog.csdn.net/Touch_Dream/article/details/73744419
getAllFiles获取文件不是按照1,2,3顺序来的,是按照1,10,101这样的顺序,可以使用sort自己排序,在不在意顺序的情况下,可以直接使用。
_findnext在64位系统操作会失败,解决方法是 //文件句柄 long hFile = 0; 需要写成intptr_t hFile

// TestForOnlyC++.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
void getAllFiles(string path, vector<string>& files)
{
	//文件句柄  
	long   hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	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)
				{
					files.push_back(p.assign(path).append("/").append(fileinfo.name));//保存文件夹名字
					getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);//递归当前文件夹
				}
			}
			else    //文件处理
			{
				files.push_back(p.assign(path).append("/").append(fileinfo.name));//文件名
			}
		} while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}
}
//测试
int main()
{
	string DATA_DIR = "E:/wangwy/GY/dataForGY/钢印3/钢印3";
	vector<string> files;
	//测试
	char * DistAll = "AllFiles.txt";
	getAllFiles(DATA_DIR, files);//所有文件与文件夹的路径都输出
	//ofstream ofn(DistAll);  //输出文件流
	int size = files.size();
	int  FaiNum = 0;
	//ofn << size << endl;
	cout<< size << endl;
	for (int i = 0; i<size; i++)
	{
		//ofn << files[i] << endl;
		cout << files[i] << endl;
	}
	//ofn.close();
	return 0;
}

另外的方法:https://blog.csdn.net/qq_40946921/article/details/91394589
opencv方法:https://blog.csdn.net/qq_31261509/article/details/79460639

2.opencv glob 顺序获取

其实opencv的glob的读取顺序也是1,10,11,101,不过这个我写好了调整顺序的函数,但有要求是命名需要纯数字。
注意路径必须用"\\"


#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/objdetect.hpp"
#include 
using namespace std;
vector<string> getOrderedNames(vector<cv::String> filenames, cv::String folder) {
	vector<string> nameArr;
	string  suffixName;
	for (size_t i = 0; i < filenames.size(); ++i)
	{
		string::size_type iPos = filenames[i].find_last_of('\\') + 1;
		string filename = filenames[i].substr(iPos, filenames[i].length() - iPos);
		string name = filename.substr(0, filename.rfind("."));
		suffixName = filename.substr(filename.rfind("."), filename.length() - 1);//获取后缀名
		nameArr.emplace_back(name);
	}
	sort(nameArr.begin(), nameArr.end(), [](string a, string b) {return stoi(a) < stoi(b); });

	vector<string> orderedFilenames;
	for (size_t i = 0; i < filenames.size(); ++i)
	{

		string orderedName = folder + "\\" + nameArr[i] + suffixName;
		orderedFilenames.emplace_back(orderedName);
		//cout << "改变后:" << orderedFilenames[i] << endl;
	}
	return orderedFilenames;
}


int main()
{
	std::vector<cv::String> filenames; // notice here that we are using the Opencv's embedded "String" class
	cv::String folder = "C:\\Users\\大喵喵\\Desktop\\\Blur\\RGB"; // again we are using the Opencv's embedded "String" class

	cv::glob(folder, filenames); // new function that does the job ;-)
	size_t count = filenames.size();
	if (count == 0)
	{
		std::cout << "file " << folder << " not  exits" << std::endl;
		return -1;
	}

	vector<string> orderedFilenames = getOrderedNames(filenames, folder);

	for (size_t i = 0; i < filenames.size(); ++i)
	{
		cout << "顺序:" << orderedFilenames[i] << endl;
	}
	
}

你可能感兴趣的:(c++,opencv,开发语言)