boost文件系统中的分段错误?

时间:2014-06-13 13:09:09

标签: c++ boost segmentation-fault boost-filesystem

我有一个函数可以迭代指定目录中的所有文件和目录 我使用了boost文件系统来做到这一点 以下是代码: -

void Utility::index(string IPath)
{
  path p(IPath) ; 
  string extension ; 
  directory_iterator end ; 
  for(directory_iterator it(p); it<end; ++it)
    {
      lastPath = it->path().string() ;
      cout<<lastPath<<endl ; 
      if(is_symlink(it->path()))
      {
        //cout<<"Found a symlink : "<<it->path()<<endl  ; 
      }
      else if (is_regular_file(it->path()))
      {
         extension = lastPath.substr(lastPath.find_last_of(".")+1) ;
         master<<lastPath<<endl ; 
       }
      else if(is_directory(it->path()))
      { 
        try
        {
          index((it->path()).string()) ;
        }
        catch(boost::filesystem3::filesystem_error e) 
        {
          cout<<e.what()<<endl ; 
        }
      }
    }
}

当我在目录“/”上运行此功能时,它会给我分段(故障) 看看gdb中的回溯,我无法理解问题所在。

回溯是: - Backtrace

1 个答案:

答案 0 :(得分:0)

取消引用或增加有效的directory_iterator can throw

it->++it的所有代码都需要位于try块中才能处理错误情况。考虑扩展现有的try块以包含该代码。

相关问题