1. 程式人生 > >wc.exe

wc.exe

返回 std pro print || word x64 c語言實現 fop

這是一個由c語言實現的統計程序,它能正確統計程序文件的字符數、單詞數、行數,以及遞歸處理目錄下符合條件的文件和返回高級選項(代碼行、空行、註釋行)。

代碼如下:

#include "stdio.h"

#include "string.h"

#include "stdlib.h" int charcalculate=0; int wordcalculate=0; int linecalculate=0; void calculate(char * file) { FILE * fp; char a; if((fp=fopen(file,"r"))==NULL)
{ printf("read file failed!\n"); exit(-1); } while(!feof(fp)) { a=fgetc(fp); if(a!=‘ ‘&&a!=‘\n‘&&a!=‘\t‘&&a!=‘,‘&&a!=‘.‘&&a!=‘!‘&&a!=‘;‘&&a!=‘=‘) charcalculate++; if(a==‘ ‘||a==‘\n‘
||a==‘\t‘||a==‘,‘||a==‘.‘||a==‘!‘||a==‘=‘||a==‘;‘) wordcalculate++; if(a==‘\n‘||a==‘\t‘) linecalculate++; } linecalculate++; charcalculate--; fclose(fp); } int main(int argc, char* argv[]) { FILE *fp; calculate(argv[2]);
while(1) { if((fp=fopen(argv[2],"r"))==NULL) { printf("FileNull\n\n\n"); scanf("%s%s%s",argv[0],argv[1],argv[2]); continue; } else if(!strcmp(argv[1],"-c")) printf("File:%sCharNum:%d\n",argv[2],charcalculate); else if(!strcmp(argv[1],"-w")) printf("File:%sWordNum:%d\n",argv[2],wordcalculate); else if(!strcmp(argv[1],"-l")) printf("File:%sLineNum:%d\n",argv[2],linecalculate); else if(!strcmp(argv[1],"exit")) { printf("Exit!\n"); break; } else printf("NullPoint\n"); printf("\n\n"); scanf("%s%s%s",argv[0],argv[1],argv[2]); } return 0; }

wc.exe