简体   繁体   中英

Find any file ending with word, c++

i want the equivalent to the regex '*bla.foo', meaning get me any files ending with bla.foo in c++.

What i came up with so far is:

#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>

    void get_all(const fs::path& root, const std::string& ext, std::vector<fs::path>& ret)
{
    if(!fs::exists(root) || !fs::is_directory(root)) return;

    fs::recursive_directory_iterator it(root);
    fs::recursive_directory_iterator endit;

    while(it != endit)
    {

        if(fs::is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename(), ext)) ret.push_back(it->path().filename());
        ++it;

    }

}

but i am getting an error in:

namespace boost {
    namespace algorithm {

        //  is_equal functor  -----------------------------------------------//

        //! is_equal functor
        /*!
            Standard STL equal_to only handle comparison between arguments
            of the same type. This is a less restrictive version which wraps operator ==.
        */
        struct is_equal
        {
            //! Function operator
            /*!
                Compare two operands for equality
            */
            template< typename T1, typename T2 >
                bool operator()( const T1& Arg1, const T2& Arg2 ) const
            {
                return Arg1==Arg2;
            }
        };

The Error is:

 /usr/local/include/boost/algorithm/string/compare.hpp:43:28: 
Invalid operands to binary expression ('const boost::filesystem::path' and 'int')

It seems like wrong arguments get passed to the function, i would like to know where i went wrong. It is my first code in c++.

Thanks!

If you want to compare string data, you need to call string() on your path object:

    if (is_regular_file(*it) && boost::algorithm::ends_with(it->path().filename().string(), ext))
        ret.push_back(it->path().filename());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM