遍历文件夹及其子孙文件夹中的文件
1 #include <QDir> 2 #include <QString> 3 #include <QDebug> 4 #include <iostream> 5 #include <fstream> 6 #include <QtCore> 7 #include <QCoreApplication> 8 9 10 std::ofstream output("D:dir.txt"); 11 12 void addSubFolderImages(QString path) 13 { 14 //判断路径是否存在 15 QDir dir(path); 16 if(!dir.exists()) 17 { 18 return; 19 } 20 21 //获取所选文件类型过滤器 22 QStringList filters; 23 filters<<QString("*.jpeg")<<QString("*.jpg")<<QString("*.png")<<QString("*.tif")<<QString("*.gif")<<QString("*.bmp"); 24 25 //定义迭代器并设置过滤器 26 QDirIterator dir_iterator(path, 27 filters, 28 QDir::Files | QDir::NoSymLinks, 29 QDirIterator::Subdirectories); 30 31 QStringList string_list; 32 while(dir_iterator.hasNext()) 33 { 34 dir_iterator.next(); 35 QFileInfo file_info = dir_iterator.fileInfo(); 36 QString absolute_file_path = file_info.absoluteFilePath(); 37 qDebug()<< absolute_file_path; 38 output << absolute_file_path.toStdString()<<std::endl; 39 string_list.append(absolute_file_path); 40 } 41 } 42 43 44 int main(){ 45 addSubFolderImages(QString("D:\\huase\\")); 46 47 qDebug()<<"COMPLETE"; 48 return 0; 49 }