在c++ boost库中使用directory_iterator列出和获取文件名的问题

problems in listing and getting file names using directory_iterator with C++ boost library

本文关键字:获取 文件名 问题 iterator boost c++ directory      更新时间:2023-10-16

我试图使用boost库中的目录迭代器,我想做一个循环,我迭代一个目录并将文件名放在一个变量中,我可以使用leaf()函数之前,但现在它已弃用。我不能使用path().filename(),甚至可以使用native()和string(),因为C编译器会给我错误。我如何修复下面的代码?

boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator file(dir);file != end_iter;++file)
{
        /*check if file is image */ 
        string fileName=file->path().filename().native(); //the error happens here
        // i can use path().filename().string() either
        boost::match_results<string::const_iterator> what;
        if (0 == boost::regex_search(fileName, what, e, boost::match_default)) 
        {
            continue;
        };
...

谢谢。

根据@Casey和@rectummelancolique的建议,在makefile中-lboost_filesystem之后添加-lboostrongystem,问题就解决了。