Boost logo

Boost :

From: gchen (chengang31_at_[hidden])
Date: 2006-10-06 02:34:51


I was not sure if this question should go to boost.user or
boost.devel, since I am testing cvs code, so I post here...

Below is a test program which count the files in sub-folders
of a given folder, if a folder has an empty folder, calling
the recursive_directory_iterator's status() will cause a
assertion failure: "attempt to dereference end iterator".

Did i missing something?

Best Regards
gchen

// begin test code===================

#include <iostream>
#include <boost\filesystem.hpp>

namespace fs = boost::filesystem;

// get file count in a folder
size_t file_count(const fs::path &p)
{
     size_t numFile = 0;
     for (fs::recursive_directory_iterator iter(p);
          iter != fs::recursive_directory_iterator(); ++iter)
     {
         if (fs::is_regular(iter->status()))
             numFile++;
     }
     return numFile;
}

int main(int argc, char *argv[])
{
     // "c:\tmp" has an empty folder "c:\tmp\empty"
     fs::path p("c:\\tmp");

     // this is OK, if test the empty folder directly
     file_count("c:\\tmp\\empty");

     for (fs::directory_iterator iter(p);
          iter != fs::directory_iterator(); ++iter)
     {
         if (fs::is_directory(iter->status()))
         {
             // file_count will cause ssertion fail when
             // encount the empty folder "c:\tmp\empty"
             size_t size = file_count(iter->path());
             std::cout << "folder: "
                       << iter->path().leaf()
                       << "has: " << size << " file(s)\n";
         }
     }
     return 0;
}

// end test code===================


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk