WC一个简单的文件检测系统 用c语言实现 不多说 上代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <io.h> 4 #include <string.h> 5 void jichu(FILE *fp1){ 6 int characters,lines,words,space,state; 7 characters=words=state=space=0; 8 lines=0; 9 char c; 10 while(( c=fgetc(fp1))!=EOF){ 11 fputc(c,stdout); 12 characters++; 13 if(c==‘\n‘){ 14 state=0; 15 lines++; 16 }if(c==‘ ‘){ 17 state=0; 18 space++; 19 }else{ 20 if(state==0){ 21 state=1; 22 words++; 23 } 24 } 25 } 26 printf("\n%d %d %d %d\n",words,lines,characters,space); 27 } 28 int klines=0,dlines=0,zlines=0,sum=0,sum1=0; 29 void kuozhan(FILE *fp1,int sum){ 30 char c; 31 while(( c=fgetc(fp1))!=EOF){ 32 if(c!=‘\n‘){ 33 if(c!=‘ ‘){ 34 sum++; 35 }if(c==‘/‘||c==‘*‘){ 36 sum1++; 37 } 38 }else{ 39 if(sum<=1){ 40 klines++;sum=0; 41 } 42 if(sum>=2&&sum1==0){ 43 dlines++; 44 }if(sum1==2){ 45 zlines++; 46 sum1=0; 47 } 48 } 49 } 50 printf("%d %d %d\n",klines,dlines,zlines); 51 } 52 void dg(){ 53 long Handle; 54 struct _finddata_t FileInfo; 55 //system("mode con: CP SELECT=936"); 56 if((Handle=_findfirst("D:\\*.txt",&FileInfo))==-1L) 57 printf("没有匹配项目\n"); 58 else{ 59 printf("%s\n",FileInfo.name); 60 while(_findnext(Handle,&FileInfo)==0) 61 printf("%s\n",FileInfo.name); 62 _findclose(Handle); 63 } 64 } 65 int main(){ 66 FILE *fp1; 67 fp1=fopen("d:\\T1.txt","r"); 68 printf("请输入c,s,a \n"); 69 char c; 70 scanf("%c",&c); 71 if(c==‘c‘){ 72 jichu(fp1); 73 }if(c==‘s‘){ 74 dg(); 75 }if(c==‘a‘){ 76 kuozhan(fp1,sum); 77 } 78 fclose(fp1); 79 80 }
由于最近事太多 所以项目完成不太理想 还有很多细节的地方要修改~anyway~等周末有空再更新把~
时间: 2024-11-01 03:57:00