1.实现前的准备
1.因为要储存学生的各种信息所以定义了一个结构体student,又因要储存很多学生,故定义结构体数组。还有要递归遍历文件夹定义的结构体
struct student {
char name[100];
int cnumber;
int hangshu;
int zongzifu;
int ifshu;
int forshu;
int whileshu;
};
struct student tongji[1000];
struct _finddata_t fileinfo;
结构体数组名为 tongji(统计),结构体中有学生姓名学号信息,.c文件的数量,.c文件有效行数,.c总字符数,.c if总数,.c for数,.c while数。 struct _finddata_t 包含在<io.h>
2.因为要往结构体中赋值申请了全局变量count,为结构体数组赋值(表达不清是这么个意思)
int count = -1;
count一开始为-1是有设计的,为什么会这么定义在后面会说明
3.main函数内容
(1)题目要求将信息放到文件中,循环遍历结构体数组,将信息写入文件中
(2)并且在main函数中输入要搜索文件路径
(3)调用了findfile函数(好戏开始)
int main()
{
char path[260];//s
printf("请输入您想搜索的文件:");
scanf("%s", path);
findfile(path); //好戏开始
FILE* fp;
fp = fopen("代码统计.txt", "w");
for (int x = 0; x <= count; x++)
{
fprintf(fp, "学生信息:%s\t\t.c文件数量%3d:\t\t总字符数:%3d\t\t有效行数:%3d\t\tif关键字的数量:%3d\t\tfor关键 字的数量:%3d\t\twhile关键字的数量:%3d\t\t平均代码行数:%3.2lf\t\t\n",
tongji[x].name, tongji[x].cnumber,
tongji[x].zongzifu, tongji[x].hangshu,
tongji[x].ifshu, tongji[x].forshu,
tongji[x].whileshu, (double)tongji[x].hangshu / tongji[x].cnumber);
}
if (count != -1)//因为怕老师不按套路出牌,来了个没意义的输入,故在此做判断
{
printf("解析成功");
}
return 0;
}
2.真正的核心部分(不会写)
1、递归遍历文件夹(这个纯抄),我水平有限实在无法独自实现这个部分,但也是代码统计最核心的部分,只能写上自己的理解
void findfile(char* path)
{
char temp[MAX_PATH] = {
0 };//初始化数组(MAX_PATH)大小为260,在windows.h
strcpy(temp, path);
strcat(temp, "\\*.*");//将传来的路径加上,至于*.*这个通过搜索资料发现是通配符(好像是这个)但由于资料不多,无法很好解释
long handle = _findfirst(temp, &fileinfo);//句柄,_findfirst这个搜索失败返回-1
if (handle == -1)
{
printf("没有此文件路径");
return;
}
while (_findnext(handle, &fileinfo) == 0)//等于0查找成功
{
if (fileinfo.attrib == _A_SUBDIR)
{
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)//好像意思是没成功找到文件夹(1)
{
continue;//继续去找
}
//这块是成功找到了文件夹
char temp[MAX_PATH] = {
0 };
strcat