C++:遍历指定路径下的文件图片

C++:遍历指定路径下的文件图片,第1张

#include 
#include 
#include 
using namespace std;

bool get_filelist_from_dir(std::string path, std::vector& files)
{
    long long  hFile = 0;
    //_findnext()第一个参数”路径句柄”,返回的类型为intptr_t(long long),
    //如果定义为long,在win7中是没有问题,但是在win10中就要改为long long或者intptr_t
    struct _finddata_t fileinfo;
    files.clear();
    if ((hFile = _findfirst(path.c_str(), &fileinfo)) != -1)
    {
        do
        {
            cout << fileinfo.name << endl;
            if (!(fileinfo.attrib & _A_SUBDIR))
            {
                files.push_back(fileinfo.name);
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
        return true;
    }
    else
    {
        return false;
    }

    //另一种写法
    //struct _finddata_t fileinfo;
    //long long handle;
    _findnext()第一个参数”路径句柄”,返回的类型为intptr_t(long long),
    如果定义为long,在win7中是没有问题,但是在win10中就要改为long long或者intptr_t
    //handle = _findfirst(path.c_str(), &fileinfo);  //返回文件句柄

//if (handle == -1) //{ // cout << "fail..." << endl; // return false; //} //else //{ // cout << fileinfo.name << endl; // files.push_back(fileinfo.name); //} //cout << "#### 1 ####" << endl; //while (!_findnext(handle, &fileinfo)) //{ // cout << "#### 2 ####" << endl; // cout << fileinfo.name << endl; //} //_findclose(handle); //return true; } vector getImageList(std::string file_path, std::string suffix) { //string file_path = "D:\\image\\"; //string suffix = "*.png"; string search_path = file_path + suffix; vector file_list;//保存遍历到的文件name if (!get_filelist_from_dir(search_path, file_list)) cout << "open file error!" << endl; vector image_path_list;//保存文件绝对路径 for (int i = 0; i < file_list.size(); i++) { image_path_list.push_back(file_path + file_list[i]); //Mat image = imread(image_path); //这里可以对图像进行处理 } return image_path_list; } int main() { vector output; string filepath = "D:\\image\\"; string suffix = "*.*";//后缀名 output = getImageList(filepath, suffix); return 0; }

涉及到了一个结构体,需要用的是name属性,当然假如需要文件大小的话也可以把size提取出来

#define _finddata_t     _finddata64i32_t

struct _finddata64i32_t
{
    unsigned    attrib;
    __time64_t  time_create;    // -1 for FAT file systems
    __time64_t  time_access;    // -1 for FAT file systems
    __time64_t  time_write;
    _fsize_t    size;
    char        name[260];
};

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562251.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存