使用boost::filesystem 3.0迭代文件

Iterating files with boost::filesystem 3.0

本文关键字:迭代 文件 filesystem boost 使用      更新时间:2023-10-16

我想迭代目录中与"keyword.txt"匹配的所有文件。我在谷歌中搜索了一些解决方案,发现如下:我可以使用Boost使用掩码来迭代目录中的文件吗?

正如我后来发现的那样,"leaf()"函数被替换了(来源:http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/index.htm->转到"不推荐的名称和功能"部分)

到目前为止,我得到的是这个,但它没有运行。很抱歉问了这个愚蠢的问题,但我或多或少是个c++初学者。

    const std::string target_path( "F:\data\" );
const boost::regex my_filter( "keyword.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
    if( !boost::regex_match( i->path().filename(), what, my_filter ) ) continue;
    // File matches, store it
    all_matching_files.push_back( i->path().filename() );
}

尝试

i->path().filename().string()

这相当于boost::filesystem 3.0 中的i->leaf()

在您的代码中:

// Skip if no match
if( !boost::regex_match( i->path().filename().string(), what, my_filter ) )     
    continue;
// File matches, store it
all_matching_files.push_back( i->path().filename().string() );