700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C++读取文件夹下文件及文件夹路径

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

时间:2024-06-27 19:59:16

相关推荐

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

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

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

Vs使用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.定义存储图像路径和标签的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 ;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。