使用boost::filesystem 3.0迭代文件
Iterating files with boost::filesystem 3.0
我想迭代目录中与"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() );
相关文章:
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 在一次迭代中从 txt 文件中读取多行
- 在文件上实现迭代器
- 在文件夹迭代上实现 RAII
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 如何从目录迭代器中选择特定文件并进行比较
- ifstream不会在下一次迭代中打开文件
- 如何从文件中读取 n 个字节并使用<uint8_t>迭代器将它们放入向量中?
- 如何使用向量 c++ 迭代文件中单词的位置
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- 使用 ofstream 迭代访问和写入多个文件
- 处理文件行流迭代器
- 迭代 txt 文件中的对象数组
- BSONCXX-是否有一种方法可以通过.bson转储文件中的文档进行迭代
- 使用 boost::filesystem 迭代目录时可以更改文件的顺序吗?
- 使用C++上的迭代读取和写入二进制文件
- 使用getline在文件中读取文件时,将值迭代值
- Cygwin目录迭代器中的Boost文件系统
- 使用boost::filesystem 3.0迭代文件