wc.exe 來源於網上。
由於沒有基礎,所以代碼來源於網上。
代碼來源http://www.cnblogs.com/changjiangcheng/p/5304120.html
github地址:https://github.com/121802102/-wc.exe/compare/master...121802102-patch-1?quick_pull=1
並且增加了擴展功能:增加了計算單詞個數和計算字符個數這兩個功能:“readWord();”“readChar()
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int c=0;
int w=0;
int l=0;
int i=0;
void calculate(char * filename)
{
FILE * fp;
char a;
if((fp=fopen(filename,"r"))==NULL)
{
printf("%s 讀取失敗!\n",filename);
exit(-1);
}
while(!feof(fp)) //設置循環,同時判斷是否到達文件尾或者發生錯誤;
{
a=fgetc(fp); //從文件中讀取一個字符;
if(a!=‘ ‘&&a!=‘\n‘&&a!=‘\t‘&&a!=‘,‘&&a!=‘.‘&&a!=‘!‘&&a!=‘;‘&&a!=‘=‘)
c++;
if(a==‘ ‘||a==‘\n‘||a==‘\t‘||a==‘,‘||a==‘.‘||a==‘!‘||a==‘=‘||a==‘;‘)
w++;
if(a==‘\n‘||a==‘\t‘)
l++;
}
l++;
c--;
fclose(fp);
}
int readData(char *filename) //此函數計算英文單詞個數
{
FILE* fp;
char ch;
char flag = 0;
int num=0;
if((fp = fopen(filename,"r")) == NULL)
{
printf("%s 讀取失敗!",filename);
exit(EXIT_FAILURE);
}
printf("%s 讀取成功!!!\n",filename);
while(!feof(fp)) //設置循環,同時判斷是否到達文件尾或者發生錯誤;
{
ch = fgetc(fp); //從文件中讀取一個字符;
if((ch >= ‘A‘ && ch <= ‘Z‘) || (ch >= ‘a‘ && ch <= ‘z‘) || (ch >= ‘0‘ && ch <= ‘9‘))
flag = 0;
else if(flag == 0 && (ch != ‘-‘ && ch != ‘/‘&& ch != ‘\‘‘))
{
num++;
flag = 1;
}
}
fclose(fp);
return num;
}
int main(int argc, char* argv[])
{
FILE *fp;
calculate(argv[2]);
while(1)
{
if((fp=fopen(argv[2],"r"))==NULL)
{
printf("沒有找到該文件!\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],c);
else if(!strcmp(argv[1],"-w"))
printf("File:%sWordNum:%d\n",argv[2],w);
else if(!strcmp(argv[1],"-l"))
printf("File:%sLineNum:%d\n",argv[2],l);
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 來源於網上。