标签:== clu get windows htm col eof pen ++
借鉴的代码及地址:http://www.cnblogs.com/zxr63/p/5304505.html
#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; }
编译时报错: [Error] stdafx.h: No such file or directory
百度到的解释是:默认情况下,VS会生成一个“stdafx.h”头文件,用来包含C、C++、Windows头文件,或者其他一些不经常变化的头文件,以加快编译速度。
于是删去#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; }
标签:== clu get windows htm col eof pen ++
原文地址:http://www.cnblogs.com/liuyelin/p/7536503.html