c++ - 使用 boost::filesystem 3.0 迭代文件

标签 c++ boost-filesystem

我想遍历目录中与“keyword.txt”匹配的所有文件。我在谷歌搜索了一些解决方案,发现了这个: Can I use a mask to iterate files in a directory with 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() ); 

关于c++ - 使用 boost::filesystem 3.0 迭代文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620348/

相关文章:

c++ - 难以置信的快速 C++ 委托(delegate)和不同的翻译单元

c++ - 我可以告诉我的类是从哪个模板实例类继承的吗?

c++ - int 是否隐式签名(C++)?

c++ - boost::filesystem、std::getenv 和并发

c++ - 如何将文件保存到可能的新目录中?

c++ - 无法编译用 Boost::Spirit 库编写的简单解析器

c++ - 使用 boost::filesystem 对目录的子文件夹和文件名进行数字排序

c++ - "C.UTF-8"Windows 上的 C++ 语言环境?

c++ - boost 文件系统的无效跨设备链接错误

c++ - 两个静态库