Question

I'm using new boost, v1.5.3, to carry out this task like following, thanks to the class recursive_directory_iterator (I don't have to write recursive code):

void ListDirRec(const char *Dir, vector<string>& DirFileList, const char* ext)
{    
recursive_directory_iterator rdi(Dir);  
recursive_directory_iterator end_rdi;

DirFileList.empty();

string ext_str0(ext);   
for (; rdi != end_rdi; rdi++)
{
    rdi++;
    //cout << (*di).path().string() << endl;
    cout << (*rdi).path().string() << endl;

    //cout << " <----- " << (*rdi).path().extension() << endl;

    //string ext_str1 = (*rdi).path().extension().string();
    if (ext_str0.compare((*rdi).path().extension().string()) == 0)
    {
        DirFileList.push_back((*rdi).path().string());
    }
}

the function list files with specific extension. This function works for cases but frequently return an "assertion fails error" like:

**** Internal program error - .... assertion (m_imp.get()) ... operations.hpp(952): dereference of end recursive_directory_iterator

I barely figure out the cause of this error. Can any try.. catch help? thanks in advance for any help

Was it helpful?

Solution

You are incrementing rdi inside the loop as well as in the for declaration:

for (; rdi != end_rdi; rdi++)
{
    rdi++;

That means that rdi might be end_rdi (the end-iterator, which means past the last element) within your loop. Is there any reason you're doing this? (If this is intentional, you should check to make sure rdi != end_rdi again after you increment it.)

OTHER TIPS

You could try something like this:

recursive_directory_iterator dir(path(Dir));
for(auto&& i : dir) {
    if (is_directory(i)) {
        //Do whatever you want
        cout << i << endl;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top