【问题标题】:Find any file ending with word, c++查找以word结尾的任何文件,c ++
【发布时间】:2017-10-24 17:32:20
【问题描述】:

我想要正则表达式“*bla.foo”的等价物,这意味着在 c++ 中获取所有以 bla.foo 结尾的文件。

到目前为止我想出的是:

#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;

    }

}

但我收到错误:

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;
            }
        };

错误是:

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

似乎错误的参数被传递给函数,我想知道我哪里出错了。这是我的第一个 c++ 代码。

谢谢!

【问题讨论】:

    标签: c++ file any


    【解决方案1】:

    如果你想比较字符串数据,你需要在你的path对象上调用string()

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

    【讨论】:

    • 谢谢,部分问题是我不知道 filename() 返回什么。你能推荐我可以轻松找到文档的来源吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2013-10-24
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多