开发者

Boost.Filesystem crashes

Does anyone had this problem ? When searching a partition with recursive_directory_iterator, when it reaches the end it crashes.

I get this in Visual Studio 2008 with boost 1.39 but also at 开发者_运维技巧home using MinGW with boost 1.46. I don't think I am doing something wrong:

#include "stdafx.h"
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;




int _tmain(int argc, _TCHAR* argv[])
{
    boost::filesystem::path musicPaths("d:\\");
    for(boost::filesystem::recursive_directory_iterator it(musicPaths); it != boost::filesystem::recursive_directory_iterator(); ++it)
    {
        string strToFind = ".mp3";
        boost::filesystem::path dummypath = it->path().filename();
        string str = dummypath.string();
        if(str.find(strToFind) != -1)
        {
            cout << str << endl;
        }
    }
    return 0;
}

EDIT:

I see that it doesnt crash at the end but when it reaches System Volume Information


Windows does not allow you to look inside the directory "System Volume Information". So unleashing a recursive_directory_iterator on "System Volume Information" is a bad idea.

Edit: You may be able to solve the problem with recursive_directory_iterator::no_push(). From The Boost docs:

void no_push(bool value=true);

    Requires: *this != recursive_directory_iterator().

    Postcondition: no_push_pending() == value.

    Throws: Nothing.

    [Note: no_push() is used to prevent unwanted recursion into a directory. --end note]


You should call no_push() before moving to the next iterator.

Pseudo code:

if(*it is a directory that I want to skip) it.no_push(); ++it;


define HDIR_DEEP_ITERATOR_BASE boost::filesystem::recursive_directory_iterator

class dir_deep_iterator : public HDIR_DEEP_ITERATOR_BASE

{

public:

.....

dir_deep_iterator & operator ++()

{

    try

    {

        return (dir_deep_iterator &)HDIR_DEEP_ITERATOR_BASE::operator ++();

    }

    catch(...)

    {

        //有些特殊的目录无法打开,比如"System Volume Information",导致抛出异常

        no_push(true);

        return (dir_deep_iterator &)HDIR_DEEP_ITERATOR_BASE::operator ++();

    }

}

....

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜