C ++ mimicking ls like commands

How to implement the ls class "filename_ [0-5] [3-4]?" how is the class? The result that I would like to save in vector .

I am currently using system () , which calls ls , but this is not portable in MS.

thanks, Arman.

+3
source share
3 answers

The following program lists the files in the current directory whose name matches the regular expression filename_[0-5][34]:

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>  // also functional,iostream,iterator,string
namespace bfs = boost::filesystem;

struct match : public std::unary_function<bfs::directory_entry,bool> {
    bool operator()(const bfs::directory_entry& d) const {
        const std::string pat("filename_[0-5][34]");
        std::string fn(d.filename());
        return boost::regex_match(fn.begin(), fn.end(), boost::regex(pat));
    }
};

int main(int argc, char* argv[])
{
    transform_if(bfs::directory_iterator("."), bfs::directory_iterator(),
                 std::ostream_iterator<std::string>(std::cout, "\n"),
                 match(),
                 mem_fun_ref(&bfs::directory_entry::filename));
    return 0;
}

For brevity, I omitted the definition transform_if(). This is not a standard feature, but it should be easy to implement.

+5

boost:: , .

boost:: regex, , , .

+2

, Windows. , FindFirstFile("*.*") , , . FindFirstFile("filename_?*.*"). (, Boost:: regex), , .

, , . , , .

0

Source: https://habr.com/ru/post/1744892/


All Articles