Boost filesystem

Hi, I am having a problem to compile this coding. While installing cygwin, I include boost filesystem package. But this code does not work. Do I have to download from boost website? Im using 32 bit windows 10. I have referred many tutorial that I found in Google. But it still does not help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>

using namespace std;
using namespace boost::filesystem;

int main()
{
    path p("D:\\");
    for (auto i = directory_iterator(p); i++)
    {
        if (!is_directory(i->path()))
        {
            cout << i->path().filename().string() << endl;
        }
        else
            continue;
    }
}
It has nothing to do with Boost. That's not how you write a for-loop.
Try instead:
for (auto i = directory_iterator(p); i != directory_iterator(); i++)

Link with -lboost_system -lboost_filesystem.
Last edited on
It occurs to me that maybe you were trying to write a range-based for loop:
1
2
3
4
5
6
7
8
9
10
// prefer uniform initialization syntax:
for (auto const& i: directory_iterator{p}) // note: that's a colon, not a semi-colon.
{ 
    if (!is_directory(i.path()))
    {
        std::cout << i.path().filename().string() << std::endl;
    }
    else
        continue;
}
Topic archived. No new replies allowed.