• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
配置文件读取(2-3)获取文件名字并保存-类的封装

 

输入

 

 

运行结果

 

 

opencv自带的

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/objdetect.hpp"

using namespace std;
using namespace cv;



void Get_path(String folder, vector<String> &filenames)
{
	glob(folder, filenames); 

	/*for (size_t i = 0; i < filenames.size(); ++i)
	{
		cout << filenames[i] << endl;
		Mat src = imread(filenames[i]);

		if (!src.data)
			cerr << "图片载入失败!!!" << endl;

		imshow("temp", src);
		waitKey(0);
	
	}*/
}

int main()
{
	//1输入参数
	vector<String> filenames1;
	String folder = "F:\\image\\image0";
	//2获取路径
	Get_path(folder, filenames1);
	//3验证地址
	for (size_t i = 0; i < filenames1.size(); ++i)
	{
		cout << filenames1[i] << endl;
	}
	system("pause");
}

  

 

自己写的

 

 

 

main.cpp

#include "Fing_path.h"




int main()
{
	

	Find_path Get_imgpath ;
	
    //1获取所有的文件名字
	// Get_imgpath.GetAllFiles(filePath, files);
	//2获取特定文件名字
    Get_imgpath.GetAllFormatFiles(Get_imgpath.filePath, Get_imgpath.files, Get_imgpath.format);
	//3保存txt
	Get_imgpath.Save_txt(Get_imgpath.Txt_path, Get_imgpath.files);


	
	return 0;
}

  Fing_path.h

 

#pragma once
#ifndef _FINDPATH_H_

#define _FINDPATH_H_
//1#ifndef...#endif 使得编译器可以根据这个名字是否被定义,再决定要不要继续编译该头文中后续的内容
//2避免头文件添加函数定义和变量声明

#include <io.h>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Find_path
{

public:

	//默认路径
	string filePath;
	//默认保存的txt路径
	string Txt_path ;
	//默认读取的文件类型
	string format ;
	
	//初始化函数
	 Find_path();
	//保存获取的文件名列表
	std::vector<std::string> files;
	//1获取所有文件名字
	 void GetAllFiles(std::string path, std::vector<std::string>& files);
	//2获取指定类型文件名字
	 void GetAllFormatFiles(std::string path, std::vector<std::string>& files, std::string format);
	//3保存成txt
	 void Save_txt(std::string Txt_path, std::vector<std::string>& files);

};



#endif // !Fing_path.cpp

  

  Fing_path.cpp

#include "Fing_path.h"

/*
功能:初始化
输入:
	无
输出:
	无
*/

 Find_path::Find_path() 
 {
 
 
 }

 /*
 功能:获取所有的文件名
 输入:
 string path,             要搜索的文件夹名字
 vector<string>& files,   用来保存获取的文件列表
 输出:
 无
 */

void Find_path::GetAllFiles(string path, vector<string>& files)
{

	intptr_t   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)  //进入文件夹查找
				{
					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(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\\").append(fileinfo.name)
			}

		} while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile); //结束查找
	}

}


/*
功能:获取特定格式的文件名
输入:
	string path,             要搜索的文件夹名字   
	vector<string>& files,   用来保存获取的文件列表
	string format            要找的特定类型文件 默认 bmp
输出:
	无
*/
void Find_path::GetAllFormatFiles(string path, vector<string>& files, string format)
{
	//文件句柄  
	intptr_t      hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("//*" + format).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) );
					GetAllFormatFiles(p.assign(path).append("//").append(fileinfo.name), files, format);
				}
			}
			else
			{
				files.push_back(p.assign(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\\").append(fileinfo.name)
			}
		} while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile);
	}
}

/*
功能:将文件名字列表保存在TXT文件
输入:
	string Txt_path           要保存的TXT文件名字
	vector<string>& files     已经获取的文件列表
输出:
	无
*/
void Find_path::Save_txt(string Txt_path, vector<string>& files)
{
	//3保存结果
	ofstream ofn(Txt_path);//打开TXT
	int size = files.size();//获取大小
	ofn << size << endl;//保存
	for (int i = 0; i<size; i++)
	{
		ofn << files[i] << endl; // 写入文件
		cout << files[i] << endl;//输出到屏幕
	}
	ofn.close();

}

  

 

posted on 2020-06-11 01:55  MKT-porter  阅读(150)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3