Recursive_directory_iterator exception thrown

I am using the recursive_directory_iterator boost iterator to recursively scan through a directory. However, when an iterator runs in a directory for which my application does not have access, an exception of the type "boost :: filesystem3 :: filesystem_error" is thrown, which stops the iterator, and the program is interrupted. In any case, I can instruct the iterator to skip such directories.

I tried the code suggested in Moving a directory with boost :: with no exceptions , however, this did not work for me. I am using boost version 1.49.

My code after executing this sentence (the best I could think of) is as follows:

void scand()
{
    boost::system::error_code ec, no_err;

    // Read dir contents recurs
    for (recursive_directory_iterator end, _path("/tmp", ec);
         _path != end; _path.increment(ec)) {

        if (ec != no_err) {
            _path.pop();
            continue;
        }
        cout << _path->path() << endl;
    }
}

Thank you, Achmed.

+5
2

boost:: filesystem (V3): https://svn.boost.org/trac/boost/ticket/4494. V2 (, std::tr2::filesystem). - .

boost::system::error_code ec;
std::deque<boost::filesystem::path> directories {initialDir};
while(!directories.empty())
{
  boost::filesystem::directory_iterator dit(directories.front(), ec);
  directories.pop_front();
  while(dit != boost::filesystem::directory_iterator())
  {
    if(boost::filesystem::is_directory(dit->path(), ec))
    {
      directories.push_back(dit->path());
    }
    HandleFile(dit->path()); // <-- do something with the file
    ++dit;
  }
}

, , .

+5

try-catch, boost:: filesystem3:: filesystem_error, :

void scand()
{
   boost::system::error_code ec, no_err;

   // Read dir contents recurs
   recursive_directory_iterator end;
   _path("/tmp", ec);
   while (_path != end) {
      try
      {
        if (ec != no_err) {
         _path.pop();
          continue;
        }
       cout << _path->path() << endl;
    }
    catch(boost::filesystem3::filesystem_error e)
    {

    }
    _path++;
   }
}
0

All Articles