0

I have a bunch of files written in mostly one programming language and also some written in others. They are in different folders, but few and targetable.

I want to make a program that says how many lines of code there are. I know I could do this for one file, just like this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream f("file.txt");
    int n = 0, aux;
    string line;
    while (getline(f, line))
        n++;
    cout << endl << n << " lines read" <<endl;
    f.close();
}

But how could I "browse" all the files and folders and read them all? Having to do it explicitly for each one of them sounds awful to me.

dabadaba
  • 9,064
  • 21
  • 85
  • 155

1 Answers1

0

Putting together what was written by others, this will be your program using Qt:

#include <iostream>
#include <fstream>
#include <QApplication>
#include <QDir>
#include <QString>

using namespace std;

int countFile (QString path)
{
    ifstream f (path.toStdString().c_str());
    int n = 0;
    string line;
    while (getline(f, line))
        n++;
    return n;
}

int countDir (QString path)
{
    int n = 0;
    QDir dir (path);
    if (dir.exists())
    {
        QFileInfoList list = dir.entryInfoList ();
        for (int i = 0; i < list.size(); ++i)
        {
            if (list[i].isDir())
                n += countDir (list[i].absoluteFilePath());
            else
                n += countFile (list[i].absoluteFilePath());
        }
    }
    return n;
}

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    cout << endl << countDir ("path/to/dir") << " lines read" <<endl;
    return 0;
}
Anton Poznyakovskiy
  • 2,109
  • 1
  • 20
  • 38