QDir problem



  • hi guys . i have one problem
    @
    #include <QCoreApplication>
    #include <QtCore>
    #include <QVector>

    void search(QString fileName)
    {
    QDir dir( fileName );
    QVector<QFileInfo> v;

    foreach (QFileInfo file, dir.entryInfoList(QDir::NoDot | QDir::NoDotDot)) // doesnt work when i use filter mode
        v.push_back( file );
    
    for (QVector<QString>::iterator itr = v.begin(); itr != v.end(); ++itr)
        qDebug() << itr->absoluteFilePath();
    
    for (QVector<QString>::iterator itr = v.begin(); itr != v.end(); ++itr)
        if ( itr->isDir() ) search( itr->absoluteFilePath() + '/' );
    

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    search( "D:/" );
    
    return a.exec&#40;&#41;;
    

    }
    @

    in drive D: i have some folder . when i use filter mode doesnt work ! i want use recursive function to iterate all file and folder in my drive D:

    tnx alot


  • Moderators

    Rethink your design. In it's current state it is not only wrong, but also very suboptimal.

    In order for a method to be recursive, you have to have some object, where results of every iteration will be stored. This can be done in many ways, probably the most common is to make the recursive method (search in your case) return a list (in your case a QVector), that is appended to the previous one with every iteration.

    Also, in main, you need to get the result, otherwise the whole endeavour is pointless.

    I know this is not what you have asked for, by the way. Just my 2 cents to make your life easier :)


  • Moderators

    As for your problem, try using QDir::NoDotAndDotDot instead :)



  • Also, take care not to follow symlinks. If you do, you might end up in an endless loop, which will result in a crash due to running out of stack space.



  • i use QDir::NoDotAndDotDot but doesnt work ! when i use filter mode QDir::entryList return nothing !! but i dont know why ! when i dont use filter mode absolutepath return the address of parent directory -(for "." and "..") - and its make the stack overflow !



  • how can i filter parent direcotory - . and .. - ? where my code is wrong ?


  • Moderators

    I'm sorry you're quite hard to understand. Will try to decode, but it might take time.

    @"where my code is wrong?" - I did give you some hints in my previous posts. Another one is this: you are iterating over every entry in "v" QVector, for every iteration of the foreach loop. This is very, very wrong (you are checking the same entry many times! And that can get you into memory problems very easily).

    I'm looking through QDir docs now, but cannot find any reason, why . and .. filters don't work for you. I remember I had some similar problem about a year ago, I'll try to find it and see what I did then. For now, you can do 2 things: manually exclude . and .. (just an if statement), and update your code according to my and Andre's hints, they will all make your life easier in the end :)



  • ok tnx a lot . you say i iterator v and its wrong !! why ?


  • Moderators

    In short and pseudocode:
    @
    foreach () {
    an element is added to v;

    for (go through ALL elements of 'v',
    including those covered by previous
    iteration of foreach):
    }
    @

    This means that in iteration 1, the second "for" will go once, in iteration 2, it will loop 2 times (including over the first element, which was already covered by 1st iteration), in 3, it will loop 3 times, and so on. If you have some nested directories below the root, it will parse them multiple times. This is a serious problem.


  • Moderators

    If you are not sure, how to create a recursive method, look it up with Google, there are plenty of examples all over the web. Getting it right is important, because recursive methods are very tricky, can lead to hard-to-debug errors and eat up loads of memory.



  • ok tnx a lot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.