zoukankan      html  css  js  c++  java
  • 使用boost库获取文件夹下所有文件名字

    最近整理项目发现一个曾经找了好久的有用的代码片段,就是获取文件夹下所有文件的名字,和当前文件的绝对路径。

    记录一下。

    使用的是boost库,

    #include <boost/filesystem.hpp>
    void getFiles(const string& rootPath,vector<string> &ret,vector<string> &name)
    { 
        namespace fs = boost::filesystem;
        fs::path fullpath (rootPath);
        fs::recursive_directory_iterator end_iter;
        for(fs::recursive_directory_iterator iter(fullpath);iter!=end_iter;iter++)
        {
            try
            {
                if (fs::is_directory( *iter ) )
                { 
                    std::cout<<*iter << "is dir" << std::endl;
                    ret.push_back(iter->path().string());
                }
                else
                {
                    string file = iter->path().string();
                    ret.push_back(iter->path().string());
                    fs::path filePath(file);
                    name.push_back(filePath.stem().string());
                }
            }
            catch ( const std::exception & ex )
            {
                std::cerr << ex.what() << std::endl;
                continue;
            }
        }   
    }
    void testLocal(string imgPath)
    {
        vector<string> nameWithPath;//绝对路径
        vector<string> imgName;//文件名字
    //get files
        getFiles(imgPath, nameWithPath, imgName);
    
        
    
        for(int i = 0; i < nameWithPath.size(); i++)
        {
            Mat image = imread(nameWithPath[i]); 
                //do something
            cout<<imgName[i]<<" "<<endl;
        } 
    
        delete detModel;
    
    }

    需要依赖的库是

  • 相关阅读:
    SpringMVC 下载本地文件
    Spring MVC 自定义转换器
    Struts,Hibernate,Spring经典面试题
    SpingMVC 执行的流程
    SpringMVC 应用配置
    SpringMVC特点
    Struts2学习
    mysql免安装配置
    mysql免安装版设置密码
    《金色梦乡》金句摘抄(六)
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/11382812.html
Copyright © 2011-2022 走看看