c语言统计文件夹中所编写.c代码行数
先实现功能,再改进
ls > allfile //列出所有文件
grep .c$ allfile > data //匹配筛选出以.c结尾文件名
接下来用统计每个文件中’\n’ 数量来统计总共编写的c代码行数
//mycount.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count = 0, ch = 0;
FILE * fp;
FILE * cp;
fp = fopen("data","r");
char name[20];
while((fscanf(fp,"%s",name))==1)
{
cp = fopen(name,"r");
while((ch=getc(cp))!=EOF)
{
if(ch == '\n')
ount++;
}
}
printf("the count %d\n",count);
}
运行如下:
疫情期间,这个文件夹写了4655行代码,感觉还是不错