使用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;

}

 

需要依赖的库是

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM