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 ;
}

版权声明:本文为qq_41375318原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。