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

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

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

Vs2019使用C++17版本

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

在这里插入图片描述

在这里插入图片描述

_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING

在这里插入图片描述

在这里插入图片描述


#include
#includenamespace 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
#include
//#include 
#includenamespace 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 dict_label;//dict_label.insert(pair("ants", 0));//dict_label.insert(pair("bees", 1));// 2.定义存储图像路径和标签的listlist<pair<string, int>> data_info;// 3.读取图像和对应label放入data_info// 遍历字典,读取图像路径和对应labelfor (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 ;
}

在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部