知识点:
一.读取文件夹内容:
-
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博客
-
统计代码行数
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); //获取文件夹下的文件名,保存在容器内 }