简易c++代码行数统计器

知识点:

一.读取文件夹内容:

  1. void findFile(string path, vector<string>& res,vector<string>& code)
    {
        string p;
        long long hFile = 0;                //文件句柄
        _finddata_t fileinfo;    //文件信息
        vector<string> folder{ path };
        //查找文件夹中的第一个文件,然后利用句柄进行遍历
        int i = 0;
        while(i<folder.size())
        {
            path = folder[i];
            vector<string> f;
            if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != 0)
            {
                do
                {
                    string path_s = p.assign(path).append("\\").append(fileinfo.name);
                    //_c(path_s);
                    const char* str = path_s.c_str();
                    const char* ptr;
                    ptr = strchr(str, '.');
                    if (ptr == nullptr)
                    {
                        f.push_back(path_s);
    
                    }
                    else if (strstr(ptr, "cpp") || strstr(ptr, "h") || strstr(ptr, "c"))
                    {
                        //_c(CountLines(path_s.c_str()));
                        num += CountLines(path_s.c_str());
                        code.push_back(path_s);
                    }
                    else
                    {
                        res.push_back(path_s);            //插入容器
                    }
                } while (_findnext(hFile, &fileinfo) != -1);
                folder = f;
        }
            
            _findclose(hFile);    //关闭文件
        }
        cout << "代码总行数:";
        _c(num);
        cout << "代码空白行数:";
        _c(blank_line);
        cout << "代码有效行数:";
        _c(num-blank_line);
    }

    使用结构体_finddata_t 定义fileinfo; long long hFile = 0;hFile为句柄,在x64下要定义为long long,为long则报错,hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)如代码中,查找当前文件路径下的所有内容并使句柄指向首地址,_findnext(hFile, &fileinfo)将句柄指向下一个文件地址,遍历文件使用广度优先,如果句柄指向的文件为文件夹,则保存到容器内准备后续搜索,如果文件为.cpp或.c或.h则统计代码行数,                        ​​​​​​​        ​​​​​​​        ​​​​​​​       访问文件部分思想源自:c++在windows下打开文件和如何访问文件夹下的所有文件_c++打开文件夹-CSDN博客

  2. 统计代码行数

    int CountLines(const char* filename)
    {
        ifstream ReadFile;
        int n = 0;
        string tmp;
        ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
        if (ReadFile.fail())//文件打开失败:返回0
        {
            return 0;
        }
        else//文件存在
        {
            while (getline(ReadFile, tmp, '\n'))
            {
                _c(tmp);
                if (tmp.length() == 0)
                {
                    blank_line++;
                }
                n++;
            }
            ReadFile.close();
            return n;
        }
    }

    使用getline读取文件中的每一行到tmp中,以‘\n’结尾,并统计空白行数,以此计算有效行数

    #include <iostream>
    #include <io.h>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    #define _c(i) std::cout<<i<<endl;
    int num = 0;
    int blank_line = 0;
    int CountLines(const char* filename)
    {
        ifstream ReadFile;
        int n = 0;
        string tmp;
        ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
        if (ReadFile.fail())//文件打开失败:返回0
        {
            return 0;
        }
        else//文件存在
        {
            while (getline(ReadFile, tmp, '\n'))
            {
                _c(tmp);
                if (tmp.length() == 0)
                {
                    blank_line++;
                }
                n++;
            }
            ReadFile.close();
            return n;
        }
    }
    
    void findFile(string path, vector<string>& res,vector<string>& code)
    {
        string p;
        long long hFile = 0;                //文件句柄
        _finddata_t fileinfo;    //文件信息
        vector<string> folder{ path };
        //查找文件夹中的第一个文件,然后利用句柄进行遍历
        int i = 0;
        while(i<folder.size())
        {
            path = folder[i];
            vector<string> f;
            if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != 0)
            {
                do
                {
                    string path_s = p.assign(path).append("\\").append(fileinfo.name);
                    //_c(path_s);
                    const char* str = path_s.c_str();
                    const char* ptr;
                    ptr = strchr(str, '.');
                    if (ptr == nullptr)
                    {
                        f.push_back(path_s);
    
                    }
                    else if (strstr(ptr, "cpp") || strstr(ptr, "h") || strstr(ptr, "c"))
                    {
                        //_c(CountLines(path_s.c_str()));
                        num += CountLines(path_s.c_str());
                        code.push_back(path_s);
                    }
                    else
                    {
                        res.push_back(path_s);            //插入容器
                    }
                } while (_findnext(hFile, &fileinfo) != -1);
                folder = f;
        }
            
            _findclose(hFile);    //关闭文件
        }
        cout << "代码总行数:";
        _c(num);
        cout << "代码空白行数:";
        _c(blank_line);
        cout << "代码有效行数:";
        _c(num-blank_line);
    }
    
    int main()
    {
        string path = "*********";       //这是文件夹的路径
        vector<string>res,code;
        findFile(path, res, code);                   //获取文件夹下的文件名,保存在容器内
    
    }
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值