我想遍历目录中与“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/