C++代码行数统计程序

该程序使用C++编写,通过DFS算法深度遍历指定路径下的文件夹,对指定后缀的文件进行行数统计。它支持多后缀过滤,并可选择是否显示文件名和行数日志。在主函数中,程序对一个示例路径进行了测试,统计了.cpp,.c,和.h文件的总行数。
摘要由CSDN通过智能技术生成
#include<io.h>
#include<iostream>
#include <fstream>
#include <vector>
using namespace std;

char line[512];
int allLine = 0;

int CountLines(string fileName)
{
	ifstream ReadFile;
	int n = 0;
	ReadFile.open(fileName.c_str(), ios::in);
	if (ReadFile.fail())
	{
		return 0;
	}
	else
	{
		string temp;
		while (ReadFile.getline(line, 512, '/n'))
		{
			n++;
		}
		
	}
	ReadFile.close();
	return n;
}

void DfsListFolderFiles(string path,string suffix="*",bool isDFS = true,bool isShowLog = true)
{
	_finddata_t file_info;
	string current_folder_path = path + "/*.*";
	
	long long handle = _findfirst(current_folder_path.c_str(), &file_info);

	if (isDFS) {
		if (-1 == handle)
		{
			return;
		}

		do
		{
			if (file_info.attrib == _A_SUBDIR)
			{
				if (strcmp(file_info.name, "..") != 0 && strcmp(file_info.name, ".") != 0)
				{
					DfsListFolderFiles(path + "/" + file_info.name, suffix);
				}
			}
		} while (!_findnext(handle, &file_info));
	}  
	

	string current_path = path + "/*." + suffix;
	handle = _findfirst(current_path.c_str(), &file_info);
	if (-1 == handle)
	{
		return;
	}

	do
	{
		//文件
		if (file_info.attrib != _A_SUBDIR)
		{
			int temp = CountLines(path + "/" + file_info.name);
			if (isShowLog)
			{
				cout << path + "/" + file_info.name << endl;
				cout << temp << endl << endl;
			}
			allLine += temp;
		}
	} while (!_findnext(handle, &file_info));
	_findclose(handle);
}

//path:路径名称(绝对路径,和相对路径)
//suffix:搜索后缀,如*  jpg,   png   等
//isDFS:是否递归文件夹
//isShowLog:是否显示文件名称和行数
void DFSFolder(string path,vector<string> suffix,bool isDFS = true,bool isShowLog = true)
{
	for (int i = 0; i < suffix.size(); i++)
	{
		DfsListFolderFiles(path, suffix[i], isDFS, isShowLog);
	}
	cout << "All Line Num:" << allLine << endl;
}

int main()
{
	//测试代码
	DFSFolder("F:/UEPROJECT/MyTower", { "cpp" ,"c", "h" },false,false);
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值