1.代码来源:http://www.cnblogs.com/zxr63/p/5304505.html
2.编译环境:VS 2013 运行环境:windows10 语言 c
3.暂无bug
4.function improvement::这个程序可以计算出文件内容的单词数,文件内容的字符数以及行数,基本功全实现了,扩展功能和高级功能还需要花时间去构思。
5.implementation: 可实现
6.github:https://github.com/Z1026395125/Helloworld2/blob/master/word%20count
代码:
#include "stdafx.h"
#include "stdio.h" #include "stdlib.h" void getWord(FILE *fp,int *num_char,int *num_word){ char ch; do{ *num_char=*num_char+1; ch=fgetc(fp); }while((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')||ch=='_'); *num_word=*num_word+1; fseek(fp,-1,SEEK_CUR); } int main(int argc, char* argv[]) { FILE *fp; char ch; int num_word=0,num_line=0,num_char=0,flag; fp=fopen("text.txt","a+"); if(fp==NULL){ printf("the file open bit"); } while(!feof(fp)){ ch=fgetc(fp); if((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')){ getWord(fp,&num_char,&num_word); } else if(ch=='\n'){ num_line++; } else if(ch=='\0'||ch=='\t'){ flag=1; } else num_char+=1; } fclose(fp); printf("字符数为:%d",num_char); printf("\n"); printf("单词数为:%d",num_word); printf("\n"); printf("行数为:%d",num_line); printf("\n"); return 0;}