遍历文件夹中所有图片

遍历文件夹中的所有图片:

必要函数的参考链接:https://www.cnblogs.com/ranjiewen/p/5960976.html

#include<iostream>
#include<io.h>  //相关API和结构体在这个库中
using namespace std;

void main()
{
	char *filename = "D:/ID_card/*.jpg";

	struct _finddata_t fileinfo;
	long handle;
	handle = _findfirst(filename, &fileinfo);  //返回文件句柄
if (handle == -1) cout << "fail..." << endl; else cout << fileinfo.name << endl;
while (!_findnext(handle, &fileinfo)) { cout << fileinfo.name << endl; } _findclose(handle); system("pause"); }

  

转载于:https://www.cnblogs.com/zf-blog/p/7864997.html

在C++中,你可以使用`std::filesystem`库来遍历文件夹并按文件名的部分前缀分组。下面是一个简单的示例,展示如何实现这个功能: ```cpp #include <iostream> #include <string> #include <vector> #include <filesystem> namespace fs = std::filesystem; // 假设图片文件名都是全路径,且以.jpg结尾 std::vector<std::vector<std::string>> group_files_by_prefix(const fs::path& folder_path) { std::vector<std::vector<std::string>> grouped_images; std::vector<std::string> current_group; for (const auto& entry : fs::directory_iterator(folder_path)) { if (!entry.is_regular_file() || !entry.path().extension() == ".jpg") { continue; // 只处理普通图片文件(.jpg) } std::string prefix = entry.path().filename().stem(); // 提取文件名的前缀 size_t common_length = std::min(prefix.size(), 10); // 我们只比较前10个字符 if (current_group.empty() || prefix.substr(0, common_length) == current_group.back().substr(0, common_length)) { // 如果当前组为空或者新文件名和上一个文件名前10个字符相同,添加到当前组 current_group.push_back(entry.path()); } else { // 否则,开始新的组 grouped_images.push_back(current_group); current_group.clear(); current_group.push_back(entry.path()); } } // 添加最后一个组,如果有的话 if (!current_group.empty()) { grouped_images.push_back(current_group); } return grouped_images; } int main() { fs::path folder_path("your_folder_path"); // 替换为实际的文件夹路径 auto image_groups = group_files_by_prefix(folder_path); for (const auto& group : image_groups) { std::cout << "Group with prefix '"; for (const auto& file : group) { std::cout << file.filename().substr(0, 10) << "... "; } std::cout << "'\n"; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值