【问题标题】:Iterating files with boost::filesystem 3.0使用 boost::filesystem 3.0 迭代文件
【发布时间】:2013-09-04 17:35:28
【问题描述】:

我想遍历与“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() );
}

【问题讨论】:

  • 错误信息是什么?

标签: c++ boost-filesystem


【解决方案1】:

试试

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

这相当于 boost::filesystem 3.0 中的 i-&gt;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() ); 

【讨论】:

  • 谢谢,但是这给了我推送功能的错误
  • 无论如何,这种方法的问题是,我需要经常在程序中搜索不同的东西,所以我更愿意交出一个“关键字”字符串。
  • 第二次,非常感谢 - 我什至理解错误:) 它运行了,但没有以预期的方式工作。我在 target_path 文件夹中有一些文件名“data1.txt”、“data2.txt”……。如果我输入 my_filter("1") 它找不到“data1.txt”。 (我确定我在正确的字典中工作)知道这是从哪里来的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2016-07-11
  • 2010-09-28
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多