使用BOOST_FOREACH遍历目录中的所有文件 [英] Iterate over all files in a directory using BOOST_FOREACH

查看:404
本文介绍了使用BOOST_FOREACH遍历目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用boost :: filesystem和BOOST_FOREACH遍历目录中的所有文件吗? 我尝试过

Can you iterate over all files in a directory using boost::filesystem and BOOST_FOREACH? I tried

path dirPath = ...
int fileCount = 0;
BOOST_FOREACH(const path& filePath, dirPath)
    if(is_regular_file(filePath))
        ++fileCount;

此代码可以编译,运行,但不会产生预期的结果.

This code compiles, runs, but does not produce the desired result.

推荐答案

您可以使用BOOST_FOREACH遍历目录中的文件,如下所示:

You can iterate over files in a directory using BOOST_FOREACH like this:

#include <boost/filesystem.hpp>
#include <boost/foreach.hpp> 

namespace fs = boost::filesystem; 

fs::path targetDir("/tmp"); 

fs::directory_iterator it(targetDir), eod;

BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod))   
{ 
    if(fs::is_regular_file(p))
    {
        // do something with p
    } 
}

这篇关于使用BOOST_FOREACH遍历目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆