Qt recursive non-recursive traversal file, qt recursive non-

Source: Internet
Author: User

Qt recursive non-recursive traversal file, qt recursive non-

The biggest advantage of qt is that it is cross-platform. Although it is said that qt is well-designed and has a wide range of documents, qt has never been so cool. However, cross-platform architecture can save a lot of manpower and material resources. "One-time compilation and everywhere compilation" also has advantages for short-term framework construction.

#include <QDebug>#include <QDir>#include <QFile>#include <QFileInfo>#include <QFileInfoList>#include <QQueue>#include <QString>#include <QStringList>#include <QVector>QVector<QString> all_files;void DfsCollectFiles(const QString& filepath)    // not recommended for deep directory{    QFileInfo curFileInfo(filepath);    if (!curFileInfo.isDir())        return;    QDir curDir(filepath);    QFileInfoList curFileInfoList(curDir.entryInfoList());    if (curFileInfoList.empty())        return;    QFileInfoList::const_iterator it;    for (it = curFileInfoList.begin(); it != curFileInfoList.end(); ++it) {        if (it->isDir() && it->fileName() != "."  && it->fileName() != "..") {            DfsCollectFiles(it->absoluteFilePath());    //recursively        } else if (!it->isDir()) {            all_files.push_back(it->absoluteFilePath());        }    }}void BfsCollectFiles(const QString& filepath)     // bfs traverse{    QFileInfo cur_fileinfo(filepath);    QQueue<QFileInfo> fileinfo_queue;    // queue    QDir cur_dir(filepath);    fileinfo_queue.enqueue(cur_fileinfo);    while (!fileinfo_queue.isEmpty()) {        cur_dir = fileinfo_queue.head().absoluteFilePath();        fileinfo_queue.dequeue();        QFileInfoList cur_fileinfolist = cur_dir.entryInfoList();        QFileInfoList::const_iterator it;        for (it = cur_fileinfolist.cbegin(); it != cur_fileinfolist.cend(); ++it) {            if (it->isDir() && it->fileName() != "." && it->fileName() != "..") {    // jump "." and ".."                fileinfo_queue.enqueue(it->absoluteFilePath());    // enqueue unresolved directories            } else if (!it->isDir()){                all_files.push_back(it->absoluteFilePath());            }        }    }}


Test code:

int main(){    QString  filepath("/home/wnn/tmp");    QVector<QString>::const_iterator it;    DfsCollectFiles(filepath);    qDebug() << all_files.size();    for (it = all_files.cbegin(); it != all_files.cend(); ++it)        qDebug() << *it;    qDebug() << endl;    all_files.clear();    BfsCollectFiles(filepath);    for (it = all_files.cbegin(); it != all_files.cend(); ++it)            qDebug() << *it;    qDebug() << all_files.size();    return 0;}

According to the naming conventions in the code above, the class name and function name are identified by the camper method. The class member functions use the "small camper method" like qt, and non-member functions use the "big camper method ", the variable names are all underlined with lowercase letters, so that the names can determine the category of a member. After traversing all the files, you can process them based on the absolute path.

Similar to listing objects using shell:
Ls-R | egrep "*. pdf" # list all pdf files

However, python is simple and crude:

import osdef cur_walk(curDir):    for parent, dirnames, filenames in os.walk(curDir):        for filename in filenames:    #files            print os.path.abspath(os.path.join(parent, filename))    for dirname in dirnames:    #directory        print os.path.abspath(os.path.join(parent, dirname))curDir = '/home/wnn/tmp'cur_walk(curDir)
http://ningning.today/2015/01/12/c++/Qt%E9%80%92%E5%BD%92%E9%9D%9E%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86%E6%96%87%E4%BB%B6/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.