0

My goal is to see how many lines in each file (.h and .cpp). I can only see the files in the current directory, sorted by size for now. I need to set path by using QString. Then, maybe QDir and QFiles can be useful. Thanks for your precious time.

QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);

QFileInfoList list = dir.entryInfoList();
qInfo() << "       Bytes Filename" << Qt::endl;
for(int i=0; i<list.size(); i++)
{
    QFileInfo fileInfo = list.at(i);
    std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
                            .arg(fileInfo.fileName()));
    std::cout << std::endl;
}
Efe Şafak
  • 25
  • 4
  • So for each file read the file to string, split the string by newline character and count the lines. And for each directory, recursively read the directory contents. It is very simple. Btw. is this a school assignment? – HiFile.app - best file manager May 04 '22 at 13:52
  • Yes it is. I am so confused because of thinking complex. Are there any chances to help me to take a little steps to reach my goal? – Efe Şafak May 04 '22 at 14:05
  • 1
    So split the task into individual steps (and write a separate function for each). For example make a function which reads a file and counts the lines. Then make a function which traverses all items in a directory, finds subdirs and calls itself for each subdir (that is the recursion). And then put these two functions together. And then you are done. Sorry, I am not going to write the assignement for you, you would learn nothing. – HiFile.app - best file manager May 04 '22 at 14:32
  • Here is how someone else recursively navigated dirs using QT. All that would be left is open'ing the file and adding to a counter. https://stackoverflow.com/questions/8052460/recursively-iterate-over-all-the-files-in-a-directory-and-its-subdirectories-in – Frebreeze May 04 '22 at 18:45

0 Answers0