我可以用口罩与升压目录遍历文件? [英] Can I use a mask to iterate files in a directory with Boost?

查看:183
本文介绍了我可以用口罩与升压目录遍历文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个目录下的东西相匹配的所有文件遍历像somefiles * .TXT。并提振::文件系统,那么内置的东西要做到这一点,或者我需要对每一片叶子正则表达式或东西()?

I want to iterate over all files in a directory matching something like "somefiles*.txt". Does boost::filesystem have something built in to do that, or do I need a regex or something against each leaf()?

推荐答案

修改:由于在评论中指出的那样,code以下的有效期为的版本提升::文件系统至V3之前。对于V3,请参阅建议的意见。

EDIT: As noted in the comments, the code below is valid for versions of boost::filesystem prior to v3. For v3, refer to the suggestions in the comments.

的boost ::文件系统没有通配符搜索,你必须自己筛选文件。

boost::filesystem does not have wildcard search, you have to filter files yourself.

这是一个code样品中提取出的目录以的boost ::文件系统内容 directory_iterator 的boost ::正则表达式过滤它

This is a code sample extracting the content of a directory with a boost::filesystem's directory_iterator and filtering it with boost::regex:

const std::string target_path( "/my/directory/" );
const boost::regex my_filter( "somefiles.*\.txt" );

std::vector< std::string > all_matching_files;

boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i )
{
    // Skip if not a file
    if( !boost::filesystem::is_regular_file( i->status() ) ) continue;

    boost::smatch what;

    // Skip if no match for V2:
    if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;
    // For V3:
    //if( !boost::regex_match( i->path().filename(), what, my_filter ) ) continue;

    // File matches, store it
    all_matching_files.push_back( i->leaf() );
}

(如果你正在寻找一个内置的目录筛选一个现成的使用类型,看看Qt的的QDir

这篇关于我可以用口罩与升压目录遍历文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆