读取代码行的数量

Reading the number of lines of code

本文关键字:取代码 读取      更新时间:2023-10-16

我有很多用一种编程语言编写的文件,还有一些文件。它们在不同的文件夹中,但很少且可目标。

我想制作一个程序,说有多少行代码。我知道我可以为一个文件做到这一点,就像这样:

#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();
}

但是我如何"浏览"所有文件和文件夹并阅读所有文件呢?必须为每个人明确地做到这一点,这对我来说听起来很糟糕。

将他人写的内容汇总在一起,这将是您的程序使用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;
}