You must read the directory tree, starting with the specified. You also need to calculate the total number of files found (excluding . And .. ). The result of the function must be recorded in a separate file. Search filters are not required because all files of the same type. My version of the implementation:

 static const char* filter = "."; QStringList create_config(QString dir_name) { QStringList ret_list; QDir dir(dir_name); QFileInfoList info_list = dir.entryInfoList(); if(info_list.size() > 2) { QList<QFileInfo>::iterator iter=info_list.begin(); QString path; for(iter=info_list.begin()+2;iter != info_list.end();iter++) { path = iter->absoluteFilePath(); if(iter->isDir()) { ret_list += create_config(path); } else { char ext[5]; strncpy(ext,path.toStdString().c_str() + (path.size() - 4),5); if(!strcmp(ext, filter))ret_list.append(path); } } } return ret_list; } 
  • write qt + cpp code and insert a piece on a pure sy ... iter->completeSuffix() == filter and of course, QString filter = "..."; - KoVadim
  • And what does not work? And why use char and strncpy in Qt code? Or everything connected with Qt you copied somewhere, but the part that you wrote yourself after else ? - ixSci
  • The part that with C ++ is from my other project, and the one that looked at Qt in Schlee. - Alex.B
  • And writing does not work in ret_list. As I understand it, after going through the directories, the entries read in the directory get into the ret_list. Then I will have to count count the number of records and everything is fine, but here’s how to get rid of it. and .. I did not understand at all. - Alex.B
  • but it seems to me that I am very keen on govnokodil ((( - Alex.B

2 answers 2

Slightly refined your code with modern C ++:

 QStringList create_config(const QString& dir_name) { QStringList ret_list; QDir dir(dir_name); QFileInfoList info_list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries); for(const auto& fileInfo : info_list) { auto path = fileInfo.absoluteFilePath(); if(fileInfo.isDir()) ret_list << create_config(path); else ret_list << path; } return ret_list; } 
  • Despite implicit sharing is it not worth passing the argument as const QString& ? And if/else I would turn it into a ternary operator. - αλεχολυτ
  • @alexolut, standing, fixed. As for the ternary operator: it looks stylishly youthful, but the code from it, in my opinion, will only suffer. - ixSci
  • one
    стильно-молодёжно :-D Considering the long line with the info_list ad info_list ternarka will, in info_list opinion, fit in quite elegantly: ret_list << fileInfo.isDir() ? create_config(path) : path; ret_list << fileInfo.isDir() ? create_config(path) : path; . Everything is pretty transparent in my opinion. Although it may need brackets. Then it is no longer ice. - αλεχολυτ
  • @alexolut, I have repeatedly heard that people have problems reading the ternary operator, so I try not to use it when it gives little. This case is just this: let it be shorter, but if / else reads faster (vertical vs. horizontal reading) - ixSci

Obtaining a complete list of files through QDir::entryInfoList() may be redundant if there are many files in separate directories and / or the task itself does not imply further work with result lines as a complete data set.

The question mentions that the result of the create_config() function should be subsequently written to a separate file. In connection with this circumstance, the use of QDirIterator can be more productive than the formation of a single (and certainly not too small) list of file names.

 int create_config(const QString &src_path, const QString &dst_fname) { QFile file(dst_fname); if(file.open(QFile::WriteOnly|QFile::Truncate|QFile::Text) == false) return -1; QTextStream stream(&file); int files_count = 0; QDirIterator itr(src_path , QDir::NoDotAndDotDot | QDir::Files , QDirIterator::Subdirectories); while(itr.hasNext()) { itr.next(); stream << itr.fileInfo().absoluteFilePath(); ++files_count; } return files_count; } 

This example function takes as arguments the path where you want to search for files, as well as the name of the output file where the list with the result will be saved. At the output, the function will return the number of found files, or "-1", if for some reason the output file could not be created / opened.

Thanks to the QDirIterator::Subdirectories iterator will QDirIterator::Subdirectories directories recursively, which is why the organization of recursion for its own function is not needed. At each moment in time there will be only one line in the memory with the file name, which allows you to crawl directories with an arbitrarily large number of files, without slowing down.