读取代码行的数量
Reading the number of lines of code
我有很多用一种编程语言编写的文件,还有一些文件。它们在不同的文件夹中,但很少且可目标。
我想制作一个程序,说有多少行代码。我知道我可以为一个文件做到这一点,就像这样:
#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;
}
相关文章:
- 读取最后一行代码算法 - c++ 时出现问题
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 如何使用指针直接从托管代码中的非托管代码中读取矢量数据
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 如何只读取和剥离特定的代码行
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 从C++代码中直接读取成员属性
- 驱动程序,以支持从 C++ 代码读取或写入 HIVE
- 读取代码中的文件版本值
- 为什么抓取窗口标题的代码会导致应用崩溃?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?
- C++ - 使用代码块从文件中读取 IP 地址
- 为什么 ifstream 文件中的换行符 - 当被此代码读取时 - 占用 2 个字节
- 如何在Qt中将文件作为二进制代码读取
- 代码读取我的循环中的额外行
- 如何在linux中使用c++代码读取AT命令
- 如何通过我的c++代码读取和传递svn版本
- 可以用c++代码读取java .class文件