使用BOOST_FOREACH迭代目录中的所有文件

时间:2021-05-30 22:47:39

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

你可以使用boost :: filesystem和BOOST_FOREACH迭代目录中的所有文件吗?我试过了

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.

此代码编译,运行,但不会产生所需的结果。

3 个解决方案

#1


29  

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

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

#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
    } 
}

#2


1  

So I guessed I missed the boat on this one, but I was having a similar issue even after I found code that theoretically should work. The issue is the boost::filesystem::path data type takes the last char off of a string.

所以我猜我错过了这个船,但即使我发现理论上应该有效的代码,我也遇到了类似的问题。问题是boost :: filesystem :: path数据类型从字符串中取出最后一个char。

I was reading from a file and my path was "c:\one\two\three". But when i made it a path data type, the string was changed to "c:\one\two\thre". No idea what that is, but due to this the file location wasn't found and blah blah blah. What i did to fix it was just add another '\' to the end. That way it removes the '\' instead of the 'e'.

我正在读取文件,我的路径是“c:\ one \ two \ three”。但是当我将它设置为路径数据类型时,字符串被更改为“c:\ one \ two \ thre”。不知道那是什么,但由于这个原因,找不到文件位置,等等等等。我做了什么修复它只是添加另一个'\'到最后。这样它就会删除'\'而不是'e'。

worked just fine after that. But as stated before, I have no idea why it did this. Hope this helps someone.

之后工作得很好。但如前所述,我不知道为什么会这样做。希望这有助于某人。

#3


0  

Your dirPath is either not a sequence, either it's sequence is of size 1.

您的dirPath要么不是序列,要么它的序列大小为1。

http://www.boost.org/doc/libs/1_48_0/doc/html/foreach.html

BOOST_FOREACH iterates over sequences. But what qualifies as a sequence, exactly? Since BOOST_FOREACH is built on top of Boost.Range, it automatically supports those types which Boost.Range recognizes as sequences. Specifically, BOOST_FOREACH works with types that satisfy the Single Pass Range Concept. For example, we can use BOOST_FOREACH with:

BOOST_FOREACH迭代序列。但究竟什么才能成为序列呢?由于BOOST_FOREACH是在Boost.Range之上构建的,因此它自动支持Boost.Range识别为序列的那些类型。具体来说,BOOST_FOREACH适用于满足单通道范围概念的类型。例如,我们可以使用BOOST_FOREACH:

  • STL containers
  • arrays
  • Null-terminated strings (char and wchar_t)
  • 以空值终止的字符串(char和wchar_t)

  • std::pair of iterators
  • std ::迭代器对

Note
The support for STL containers is very general; anything that looks like an STL container counts. If it has nested iterator and const_iterator types and begin() and end() member functions, BOOST_FOREACH will automatically know how to iterate over it. It is in this way that boost::iterator_range<> and boost::sub_range<> work with BOOST_FOREACH.

注意对STL容器的支持非常普遍;任何看起来像STL容器的东西都很重要。如果它嵌套了迭代器和const_iterator类型以及begin()和end()成员函数,BOOST_FOREACH将自动知道如何迭代它。通过这种方式,boost :: iterator_range <>和boost :: sub_range <>可以与BOOST_FOREACH一起使用。

#1


29  

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

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

#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
    } 
}

#2


1  

So I guessed I missed the boat on this one, but I was having a similar issue even after I found code that theoretically should work. The issue is the boost::filesystem::path data type takes the last char off of a string.

所以我猜我错过了这个船,但即使我发现理论上应该有效的代码,我也遇到了类似的问题。问题是boost :: filesystem :: path数据类型从字符串中取出最后一个char。

I was reading from a file and my path was "c:\one\two\three". But when i made it a path data type, the string was changed to "c:\one\two\thre". No idea what that is, but due to this the file location wasn't found and blah blah blah. What i did to fix it was just add another '\' to the end. That way it removes the '\' instead of the 'e'.

我正在读取文件,我的路径是“c:\ one \ two \ three”。但是当我将它设置为路径数据类型时,字符串被更改为“c:\ one \ two \ thre”。不知道那是什么,但由于这个原因,找不到文件位置,等等等等。我做了什么修复它只是添加另一个'\'到最后。这样它就会删除'\'而不是'e'。

worked just fine after that. But as stated before, I have no idea why it did this. Hope this helps someone.

之后工作得很好。但如前所述,我不知道为什么会这样做。希望这有助于某人。

#3


0  

Your dirPath is either not a sequence, either it's sequence is of size 1.

您的dirPath要么不是序列,要么它的序列大小为1。

http://www.boost.org/doc/libs/1_48_0/doc/html/foreach.html

BOOST_FOREACH iterates over sequences. But what qualifies as a sequence, exactly? Since BOOST_FOREACH is built on top of Boost.Range, it automatically supports those types which Boost.Range recognizes as sequences. Specifically, BOOST_FOREACH works with types that satisfy the Single Pass Range Concept. For example, we can use BOOST_FOREACH with:

BOOST_FOREACH迭代序列。但究竟什么才能成为序列呢?由于BOOST_FOREACH是在Boost.Range之上构建的,因此它自动支持Boost.Range识别为序列的那些类型。具体来说,BOOST_FOREACH适用于满足单通道范围概念的类型。例如,我们可以使用BOOST_FOREACH:

  • STL containers
  • arrays
  • Null-terminated strings (char and wchar_t)
  • 以空值终止的字符串(char和wchar_t)

  • std::pair of iterators
  • std ::迭代器对

Note
The support for STL containers is very general; anything that looks like an STL container counts. If it has nested iterator and const_iterator types and begin() and end() member functions, BOOST_FOREACH will automatically know how to iterate over it. It is in this way that boost::iterator_range<> and boost::sub_range<> work with BOOST_FOREACH.

注意对STL容器的支持非常普遍;任何看起来像STL容器的东西都很重要。如果它嵌套了迭代器和const_iterator类型以及begin()和end()成员函数,BOOST_FOREACH将自动知道如何迭代它。通过这种方式,boost :: iterator_range <>和boost :: sub_range <>可以与BOOST_FOREACH一起使用。