2014-01-30 71 views
0

我有大量的文件写在主要是一种编程语言,也有一些写在别人。他们在不同的文件夹中,但很少和可定位。阅读代码行数

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

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

但我怎么可能“浏览”所有文件和文件夹,并阅读所有?必须明确地为它们中的每一个做这件事对我来说听起来很糟糕。

+0

必须使用C++来做到这一点?为此目的有更好的语言。 – waTeim

+0

@waTeim我也可以使用java或php,但我不知道更多的语言。 – dabadaba

+0

我一直在思考利用bash等命令“wc” – waTeim

回答

0

放在一起写着什么其他人,这将是使用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; 
}