我有一个函数可以迭代指定目录中的所有文件和目录。
为此,我使用了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中的回溯,我无法理解问题出在哪里。
回溯是:-

发布于 2014-06-13 22:21:56
取消引用或递增有效的directory_iterator can throw。
所有使用it->或++it的代码都需要在try块中以处理错误条件。考虑扩展现有的try块以包含该代码。
https://stackoverflow.com/questions/24206142
复制相似问题