我试图写我在C首先++编程,我需要使用Boost库。我试图写一个程序,递归的目录树中去,并返回的最新日期和最早的文件。

这是我现在是:

#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;
}

我遇到的问题是:

1。当我遇到过长路径(超过256首或260个字符,我想),有一个错误:

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

2,当我与非accessable目录,例如“系统卷信息”相遇,我有两个更多:

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

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

如何修改上面的代码来处理Windows下的长路径名?是真的很难做吗?一些程序,如总指挥官例如与长路径没有问题,但许多程序仍然有。

更重要的问题是,怎样才能我实际上使上述代码工作(不关心长路径)。问题是,当for ( ; it_dir != it_end; ++it_dir )与不可访问目录满足,它抛出一个异常,捕获这个异常,我需要定义外抓。但是,当我以外就意味着的的周期不持续。因此,这意味着在上面的代码工作就先不要访问的文件夹。在那里,它会引发异常并结束。

有没有办法回去到的异常后周期已经抛出? 我的想法是做一个++ it_dir渔获内再次启动的周期。但是,我怎么能重新开始吗?壳我将其移出到一个单独的函数?

很抱歉,如果我的理解是不明确的,这是我的第一个项目。我从来没有使用C ++之前,但我想我最好的!

编辑:

任何其他答案?问题是,美中不足的是没有工作的的周期为“无法访问”样的错误。我怎样才能使它在里面工作?这里是产生误差最小的代码。有没有办法赶上这个错误里面的循环?或者捕捉它的方式,它可以与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
  }
}
有帮助吗?

解决方案

原来它是在升压中的错误。我发现了一个bug支持票,并促成了它。

其他提示

只要把try / catch语句内的for循环...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top