开发者

questions about my first program using Boost libraries (exceptions, long path)

开发者 https://www.devze.com 2023-01-26 20:58 出处:网络
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

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) << " " 开发者_JAVA百科<< 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...

0

精彩评论

暂无评论...
验证码 换一张
取 消