参考:https://blog.csdn.net/sazass/article/details/100163264

TraverseDirectory可以遍历多级目录

getFiles不能遍历多级目录

#include "stdafx.h"#include <stdio.h>
#include <tchar.h>#include <vector>
#include <algorithm>
#include <fstream>using namespace std;
using namespace caffe;
typedef unsigned char BYTE;#include <io.h>
#include <stdio.h>int endsWith(string s, string sub) {return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0;
}
void getFiles(string path, vector<string>& files, string postfix)
{intptr_t hFile = 0;struct _finddata_t fileinfo;string p;if ((hFile = _findfirst(p.assign(path).c_str(), &fileinfo)) != -1){do{if ((fileinfo.attrib & _A_SUBDIR)){if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)getFiles(p.assign(path).append("\\*"), files, postfix);}else{string filename = fileinfo.name;if (postfix.size() == 0 || (postfix.size() > 0 && endsWith(filename, postfix) && (postfix.c_str(), strchr(fileinfo.name, '.'))))files.push_back(p.assign(fileinfo.name));}} while (_findnext(hFile, &fileinfo) == 0);_findclose(hFile);}
}bool TraverseDirectory(std::string path, vector<string>& vec_path, string postfix)
{__int64  Handle;struct __finddata64_t  FileInfo;string strFind = path + "\\*";if ((Handle = _findfirst64(strFind.c_str(), &FileInfo)) == -1L){printf("没有找到匹配的项目\n");return false;}do{//判断是否有子目录if (FileInfo.attrib & _A_SUBDIR){//判断是子文件夹//下面的判断条件很重要,过滤 . 和 ..if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0)){string newPath = path + "\\" + FileInfo.name;TraverseDirectory(newPath, vec_path, postfix);}}else   //判断是文件{string newPath = path + "\\" + FileInfo.name;//自定义操作if (postfix.size() == 0 || (postfix.size() > 0 && endsWith(newPath, postfix) && (postfix.c_str(), strchr(newPath.c_str(), '.'))))vec_path.push_back(newPath);}} while (_findnext64(Handle, &FileInfo) == 0);_findclose(Handle);return true;
}#include <string.h>
#include <Windows.h>
char sBuf[1024];
char *ptr;
int main(int argc, char* argv[]) {if (caffe::GPUAvailable()) {caffe::SetMode(caffe::GPU, 0);}if (GetModuleFileNameA(NULL, sBuf, sizeof(sBuf))){ptr = strrchr(sBuf, '\\');if (ptr)*ptr = '\0';SetCurrentDirectoryA(sBuf);}int result= init("model");int detect_model= 1;std::vector<std::string> files_1;getFiles(R"(D:\BaiduNetdiskDownload\CASIA\CASIA-WebFace)", files_1, ".jpg");vector<string> vec_path;TraverseDirectory(R"(D:\BaiduNetdiskDownload\CASIA\CASIA-WebFace)", files_1, ".jpg");

只能遍历一级目录


//created:2020.04.06 by Andison#include<iostream>
#include<vector>
#include<algorithm>
#include <opencv2/opencv.hpp>
using namespace std;
//读取路径下的特定格式文件的路径,返回按文件名升序排列的文件路径vector
int getFilePaths(vector<string> &filepaths, cv::String filePath);int main()
{vector<string> filePaths;cv::String folderPath = "C:\\Users\\sun\\Desktop\\paths\\*.txt";double t1 = cv::getTickCount();getFilePaths(filePaths, folderPath);double t2 = cv::getTickCount();cout << "Time elapsed: " << 1000 * (double)(t2 - t1) / cv::getTickFrequency() <<" ms."<< endl;getchar();return 0;
}
// 名称按升序排列,来源博客: https://blog.csdn.net/sss_369/article/details/87740843
int  getFilePaths(vector<string> &filepaths, cv::String filePath)
{filepaths.clear();cout << "Read files from: " << filePath << endl;vector<cv::String> fn;cv::glob(filePath, fn, false);if (fn.size() == 0){cout << "file " << filePath << " not  exits" << endl;return -1;}//prepare pair for sort vector<pair<int, string>> v1;pair<int, string> p1;vector<cv::String >::iterator it_;for (it_ = fn.begin(); it_ != fn.end();    ++it_){   //1.获取不带路径的文件名,1.txtstring::size_type iPos = (*it_).find_last_of('\\') + 1;string filename = (*it_).substr(iPos, (*it_).length() - iPos);//2.获取不带后缀的文件名,1string name = filename.substr(0, filename.rfind("."));//3.构建键和值的pairtry {//防止文件夹中出现非整数的文件名导致的错误p1 = make_pair(stoi(name), (*it_).c_str());}catch(exception e){cout << "Crushed -> " << e.what() << endl;//continue; 直接continue一样 it_ = fn.erase(it_);//https://www.cnblogs.com/shiliuxinya/p/12219149.htmlit_--; //erase函数的返回的是指向被删除元素的下一个元素的迭代器,所以执行erase()后要把迭代器减1,指向前面一个}v1.emplace_back(p1);}//cout << "v1.sie(): " << v1.size()<<endl;sort(v1.begin(), v1.end(), [](auto a, auto b) {return a.first < b.first; });vector<pair<int, string> >::iterator it;for (it = v1.begin(); it != v1.end(); ++it){//cout << it->first << endl;//cout << it->second << endl;filepaths.emplace_back(it->second);}return 0;
}

c++ 遍历多级目录相关推荐

  1. C++Windows下递归遍历多级目录

    #include <tchar.h> #include <Windows.h> #include <Shlwapi.h> #pragma comment( lib, ...

  2. python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法

    本文实例讲述了Python实现多级目录压缩与解压文件的方法.分享给大家供大家参考,具体如下: 咱向来就是拿来主意,也发个东西供同行"拿来"使用吧 咱信奉的就是少量的代码完成大量的工 ...

  3. python读取路径中字符串_python实现提取str字符串/json中多级目录下的某个值

    字符串多级目录取值: 比如说: 你response接收到的数据是这样的. 你现在只需要取到itemstring 这个字段下的值.其他的都不要! 思路就是:字符串是个json格式(或转为json格式), ...

  4. python层级抓取_python实现提取str字符串/json中多级目录下的某个值

    字符串多级目录取值: 比如说: 你response接收到的数据是这样的. 你现在只需要取到itemstring 这个字段下的值.其他的都不要! 思路就是:字符串是个json格式(或转为json格式), ...

  5. 广工操作系统课设--多用户多级目录的文件系统

    广东工业大学课程设计任务书 一.课程设计的内容 本课程设计要求设计一个模拟的多用户多级目录的文件系统.通过具体的文件存储空间的管理.文件的物理结构.目录结构和文件操作的实现,加深对文件系统内部功能和实 ...

  6. 根据H标签自动生成多级目录

    根据H标签自动生成多级目录 这个问题分为两步 将标签的层级关系生成树结构 递归遍历树结构,使用ol li嵌套生成多级目录 <h2>一级标题1</h2> 1. 一级标题1 < ...

  7. File 删除多级目录下的文件和文件夹

    前提 删除多级目录 必须保证先删除文件 再删除空的文件夹 //删除 E盘itcast文件夹下所有内容File ff = new File("E:\\itcast");delectF ...

  8. 网上商城多级目录展示

    像这样的多级目录是怎样做成的呢,其实也很简单,数据库只需要一个表就能把这三级目录显示在页面上! 软件环境 : 开发工具STS ,Tomcat 8.5,mysql5.5 一.先看看UI人员设计的html ...

  9. c++ 判断文件夹是否存在,不存在则创建(可建多级目录)

    c++中,<io.h>中的_access可以判断文件是否存在,<direct.h>中的_mkdir可以创建文件. 建单级目录: #include <io.h> #i ...

最新文章

  1. vb 怎样指定 dll 引用路径_C#/VB.NET 比较两个Word文档差异
  2. Android 个人学习笔记- 导入android项目,无法自动生成R文件的解决方法
  3. Arduino(新手之路1)
  4. 编程之美-程序理解和时间分析整理
  5. mysql connector python linux_Python使用mysql.connector链接mysql数据库
  6. PDF Reader
  7. nodejs-- vuex中mapActions
  8. finereport字段显示设置_如何在Excel中显示和编辑中文拼音字段
  9. 信息学奥赛一本通 1033:计算线段长度 | OpenJudge NOI 1.3 16
  10. php自动获取节气对应的年月日_php 根据时间获取二十四节气,返回json
  11. 乱码插入mac mysql汉字乱码问题解决
  12. 【面向对象设计原则】之依赖倒置原则(DIP)
  13. python开发ios程序_使用Python开发iOS程序
  14. flash插件java_[Java教程]SWFObject Flash 增强插件
  15. python爬取淘宝数据魔方_淘宝数据魔方看人群情况
  16. java当前日期_Java 获取当前日期的四种方法
  17. vue实现抽奖大转盘
  18. 程序员生存定律-打造属于自己的稀缺性
  19. python3 jason 、pickle 和cpickle
  20. Python时间序列LSTM预测系列教程(6)-单变量

热门文章

  1. 国际化困境(第一篇)
  2. linux c 通过套接字获取本地远程地址信息 getsockname getpeername 简介
  3. Linux C编程--string.h函数解析
  4. Java 语言 集合架构(Set规则集)
  5. 无盘服务器pnp,深入解读无盘PNP方法,无盘系统PNP并不神秘
  6. gin post 数据参数_Gin 使用示例(四):绑定查询字符串或 POST 数据
  7. 为什么ppt图形卡配置不正确_电脑配置 | 赛博朋克2077什么配置能玩
  8. android 常用的监听器,Android中的Keyboard监听事件
  9. 宝塔如何备份网站_学习织梦网站必需会的一件事:织梦网站数据备份
  10. 多线程面试题_线程,代码和数据–多线程Java程序实际运行的方式