标签:
/*统计文件的行数, 单词数, 字符数, */ #include <stdio.h> int line(FILE *op); int word(FILE *op); int main(int argc, char const *argv[]) { FILE *op = NULL; int string = 0; if((op=fopen("./statistics.c", "r"))==NULL)//打开的是本身这个文件. { printf("Error!\n"); return -1; } while(!feof(op)) { if(fgetc(op)!=EOF) string++; } printf("\n 行数=%d\n 单词=%d\n 字符=%d\n ", line(op), word(op), string); fclose(op); return 0; } /*行数函数*/ int line(FILE *op) { fseek(op, 0, SEEK_SET); int line = 0; while(!feof(op)) { if(fgetc(op)==‘\n‘) line++; } return line; } /*单词数函数*/ int word(FILE *op) { fseek(op, 0, SEEK_SET); int word = 0; int judge = 0; int ch = 0; while(!feof(op)) { ch = fgetc(op); if(ch==‘ ‘&&judge==1) { judge = 0; word++; } else if(ch!=‘ ‘) judge = 1; } return word; }
标签:
原文地址:http://my.oschina.net/dengwo/blog/499395