#include
#include
#include
#include
#include
using namespace std;
void split(const string& str, const string& sp, vector& vec)
{
size_t size = sp.size();
vec.clear();
size_t end = 0, start = 0;
while (start != string::npos && start < str.size())
{
end = str.find(sp, start);
vec.push_back(str.substr(start, end - start));
start = end == string::npos ? end : end + size;
}
if (vec.empty())
vec.push_back(str);
}
void GetSpecialFilesFromDirectory(string path, string fileType, vector& files)
{
vector tempFileTypes;
split(fileType, " ", tempFileTypes);
if (tempFileTypes.size() == 0)
return;
for (int i=0;i
{
// 文件句柄
long hFile = 0;
// 文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*" + tempFileTypes[i]).c_str(), &fileinfo)) != -1) {
do
{
// 第一种:保存文件的全路径
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
//第二种:不保存文件的全路径
//files.push_back(fileinfo.name);
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
}
void GetAllFilesIncludeSubfolder(string path,string fileType,vector& files)
{
vector tempFileTypes;
split(fileType, " ", tempFileTypes);
if (tempFileTypes.size() == 0)
return;
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{ //比较文件类型是否是文件夹
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
string subForld = p.assign(path).append("\\").append(fileinfo.name);
//递归搜索
GetSpecialFilesFromDirectory(subForld, fileType, files);
GetAllFilesIncludeSubfolder(p.assign(path).append("\\").append(fileinfo.name),fileType, files);
}
}
else
{
for (int i=0;i
{
string fileName = fileinfo.name;
if (fileName.find(tempFileTypes[i]) != std::string::npos)
{
// 第一种:保存文件的全路径
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
//第二种:不保存文件的全路径
//files.push_back(fileinfo.name);
}
else
{
continue;
}
}
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
std::string GetFileContent(const std::string& filepath)
{
std::ifstream infile;
infile.open(filepath);
std::string fileContent = "";
if (infile.is_open())
{
std::string tempString;
while (getline(infile, tempString))
{
fileContent += tempString;
}
infile.close();
}
else
{
std::cout << "Fail to open the config file:" << filepath << std::endl;
}
return fileContent;
}
int main()
{
int codeLineCount = 0;
vector m_Files;
string path = "E:\\test\\src";
GetAllFilesIncludeSubfolder(path, ".h .cpp", m_Files);//这里以空格分割文件后缀名
ofstream outputFile;
outputFile.open("C:\\Users\\Administrator\\Desktop\\CodeContent.txt",ios::out);
for (int i=0;i
{
std::ifstream infile;
infile.open(m_Files[i]);
if (infile.is_open())
{
std::string tempString;
while (getline(infile, tempString))
{
//软著只需要3000行代码
/*if (codeLineCount > 3000)
{
break;
}*/
if(tempString.find("//")!= string::npos || tempString.find("/*") != string::npos)
{
cout << "注释" << endl;
continue;
}
else if(tempString.length() == 0)
{
cout << "空行" << endl;
continue;
}
else
{
outputFile << tempString << endl;
codeLineCount++;
}
}
infile.close();
}
else
{
std::cout << "Fail to open the config file:" << m_Files[i] << std::endl;
}
}
outputFile.flush();
outputFile.close();
cout << "项目代码总行数 = " << codeLineCount << endl;
return 0;
}