Fork me on GitHub

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

}

 

需要依赖的库是

 

posted @ 2019-08-20 14:43  hellowOOOrld  阅读(3242)  评论(0编辑  收藏  举报