纯cpp实现:获取某个目录下面所有相同类型文件的路径字符串

做图像处理验证算法过程中,经常需要读取大量图片,用opencv,有的版本还不带有 contri.h 这个头文件

就无法使用它提供的Directory这个类,网上找到了一个很不错的用法,纯cpp的,这样多好,不受平台的

限制,感谢

 

需要添加的头文件

1 #include <iostream>
2 #include <io.h>
3 #include <vector>
4 #include <fstream>

 

具体代码:

 1 void get_all_file_path(string path, vector<string>&files, string fileType) {
 2 
 3     //文件句柄
 4     long hFile = 0;
 5     struct _finddata_t  fileInfo;
 6     string p;
 7 
 8     if ((hFile = _findfirst(p.assign(path).append("\\*" + fileType).c_str(), &fileInfo)) != -1) {
 9         do {
10             files.push_back(p.assign(path).append("\\").append(fileInfo.name));
11         } while (_findnext(hFile, &fileInfo) == 0);
12 
13         _findclose(hFile);//关闭句柄
14 
15     }
16 
17 }

 

使用:

 1 const string single_images_dir = "H:\\002_SenseLine\\month07\\00_images\\000_singles\\";
 2 std::vector<std::string> file_names;
 3 
 4 get_all_file_path(single_images_dir, file_names, ".bmp");
 5 
 6 for (int i = 0; i < file_names.size(); i++)
 7 {
 8     string file_path = file_names[i];
 9     Mat img = imread(file_path);
10     imshow("原图", img);    
11 }

 

posted @ 2018-07-30 16:58  仆人  阅读(323)  评论(0编辑  收藏  举报