문제

I am trying to write my first program in C++, and I need to use Boost library. I am trying to write a program which recursively goes through a directory tree and returns the date of the newest and the oldest file.

This is where I'm now:

#define BOOST_FILESYSTEM_VERSION 3

#include "boost/filesystem.hpp"
#include <iostream>
#include <ctime>


using namespace std;
namespace fs = boost::filesystem;


int main() {
 fs::recursive_directory_iterator it_end;
 fs::recursive_directory_iterator it_dir("e:\\");
 fs::path p;

 time_t oldest( time(NULL) );
 time_t newest(0);

 try {
  for ( ; it_dir != it_end; ++it_dir ) {
   p = *it_dir;
   try {
    time_t t( last_write_time(p) );
    if (t<oldest) oldest=t;
    if (t>newest) newest=t;
    if (fs::is_directory(p)) cout << (p) << " " << t << endl;
   } 

   catch (const fs::filesystem_error& ex) {
    cout << "\n" << ex.what() << "\n";
   }
  }
 }

 catch (const fs::filesystem_error& ex) {
  cout << "\n" << ex.what() << "\n";
 }

 cout << "\nOldest: " << ctime(&oldest);
 cout << "Newest: " << ctime(&newest) << endl;

 return 0;
}

The problems I've met are that:

1.When I encounter a too long path (more than 256 or 260 characters, I think), there is an error:

boost::filesystem::last_write_time: The system cannot find the path specified:

2.When I meet with a non accessable directory, like "System Volume Information", I have two more:

boost::filesystem::last_write_time: Access is denied: "e:\System Volume Information"

boost::filesystem::directory_iterator::construct: Access is denied: "e:\System Volume Information"

How can I modify the code above to handle long path names under Windows? Is it really hard to do it? Some programs, like Total Commander for example has no problems with long paths, but many programs still have.

The more important question is that how can I actually make the above code work (not caring about long paths). The problem is that when for ( ; it_dir != it_end; ++it_dir ) meets with a not accessible directory, it throws an exception, and to catch this exception, I need to define the outside catch. But when I'm outside it means that the for cycle is not continuing. So it means that the above code works as far as the first not accessible folder. There it throws an exception and ends.

Is there any way to go back into the for cycle after an exception has been thrown? My idea is to do a ++it_dir inside the catch and start the for cycle again. But how can I start it again? Shell I move it out to a separate function?

Sorry if my understanding is not clear, it's my first project. I never used C++ before but I'm trying my best!

EDIT:

Any other answer? The problem is that the catch is not working inside the cycle for "not accessible" kind of errors. How can I make it work inside? Here is the smallest code producing the error. Is there any way to catch this error inside the for cycle? Or catch it in a way that it could continue after skipping the non-accessible element with a it_dir++?

int main() {
 fs::recursive_directory_iterator it_end;
 fs::recursive_directory_iterator it_dir("e:\\");

  for ( ; it_dir != it_end; ++it_dir ) {
  //something here
  }
}
도움이 되었습니까?

해결책

It turned out it is a bug in boost. I've found a bug support ticket for it and contributed to it.

다른 팁

Just put the try/catch inside the for loop...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top