C++读取文件夹下文件及文件夹路径

C++读取文件夹下文件及文件夹路径

本文使用namespace fs = std::filesystem;需要使用C++17版本

Vs2019使用C++17版本

如果要坚持使用c++14,则加入预处理中宏定义:_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING

在这里插入图片描述

在这里插入图片描述

_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING

在这里插入图片描述

在这里插入图片描述


#include<iostream>
#include<filesystem>

namespace fs = std::filesystem;
using namespace std;


int main() {


 for (const auto& entry : fs::directory_iterator("D:\\dataset\\hymenoptera_data\\val"))
     std::cout << entry.path() << std::endl;

}

在这里插入图片描述

Path操作

fs::path currentPath = fs::current_path();

//root_name 返回路径的根名
std::cout << "root_name          = " << currentPath.root_name() << std::endl;
//root_directory 返回路径的根目录
std::cout << "root_directory     = " << currentPath.root_directory() << std::endl;
//root_path 返回路径的根路径
std::cout << "root_path          = " << currentPath.root_path() << std::endl;
//relative_path 返回相对根路径的路径
std::cout << "relative_path      = " << currentPath.relative_path() << std::endl;
//parent_path 返回亲路径的路径
std::cout << "parent_path        = " << currentPath.parent_path() << std::endl;
//filename 返回文件名路径组分
std::cout << "filename           = " << currentPath.filename() << std::endl;
//stem 返回主干路径组分
std::cout << "stem               = " << currentPath.stem() << std::endl;
//extension 返回文件扩展名路径组分
std::cout << "extension          = " << currentPath.extension() << std::endl;
std::cout << "extension          = " << makefilePath.extension() << std::endl;

//查询操作
//empty 检查路径是否为空
std::cout << "empty              = " << currentPath.empty() << std::endl;
//检查对应路径元素是否非空
std::cout << "has_root_path      = " << currentPath.has_root_path() << std::endl;
std::cout << "has_root_name      = " << currentPath.has_root_name() << std::endl;
std::cout << "has_root_directory = " << currentPath.has_root_directory() << std::endl;
std::cout << "has_relative_path  = " << currentPath.has_relative_path() << std::endl;
std::cout << "has_parent_path    = " << currentPath.has_parent_path() << std::endl;
std::cout << "has_filename       = " << currentPath.has_filename() << std::endl;
std::cout << "has_stem           = " << currentPath.has_stem() << std::endl;
std::cout << "has_extension      = " << currentPath.has_extension() << std::endl;

//检查 root_path() 是否唯一标识文件系统位置
std::cout << "is_absolute        = " << currentPath.is_absolute() << std::endl;
std::cout << "is_relative        = " << currentPath.is_relative() << std::endl;



示例:

#include<iostream>
#include<map>
//#include <torch/torch.h>
#include<filesystem>

namespace fs = std::filesystem;
using namespace std;


void printList(const list<pair<string, int>>& list1) {

	for (list<pair<string, int>>::const_iterator it = list1.begin(); it != list1.end(); it++) {
		cout << it->first << "  " << it->second << endl;
	}
	cout << endl;
}


list<pair<string, int>> get_imgs_labels(const std::string& data_dir, map<string, int> dict_label)
{
	// 1.定义标签
	//map<string, int> dict_label;
	//dict_label.insert(pair<string, int>("ants", 0));
	//dict_label.insert(pair<string, int>("bees", 1));
	// 2.定义存储图像路径和标签的list
	list<pair<string, int>> data_info;
	// 3.读取图像和对应label放入data_info
	// 遍历字典,读取图像路径和对应label
	for (map<string, int>::iterator it = dict_label.begin(); it != dict_label.end(); it++)
	{
		// 遍历目录查找
		for (const auto& file_path : fs::directory_iterator(data_dir))
		{
			if (file_path.path().filename() == it->first) {
				// 遍历所有图像路径
				for (const auto& img_path : fs::directory_iterator(data_dir + "\\" + it->first))
				{
					//std::cout << img_path.path() << std::endl;
					data_info.push_back(pair<string, int>(img_path.path().string(), it->second));
				}
			}
			//std::cout << entry.path() << std::endl;
		}
		//printList(data_info);
	}

	return data_info;
}

int main(){
	map<string, int> dict_label;
	dict_label.insert(pair<string, int>("ants", 0));
	dict_label.insert(pair<string, int>("bees", 1));

	auto a= get_imgs_labels("D:\\dataset\\hymenoptera_data\\val", dict_label);
	printList(a);
	int a1111;
	cin >> a1111 ;
}

在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值