博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
统计文本中字符数和单词数
阅读量:4116 次
发布时间:2019-05-25

本文共 2534 字,大约阅读时间需要 8 分钟。

#include"count_text.h"#include 
#include
int main(int argc,char* argv[]){ char* file = NULL; FILE* fp = NULL;//文件指针,指向file文件 fp = fopen(argv[1], "rb");//只读 fseek(fp, 0L, SEEK_END);//定位到文件末尾 int size = ftell(fp);//从文件开始到末尾的的字节数 file = (char*)malloc(size+1); memset(file, 0, size+1); strcpy(file, argv[1]); fp = fopen(file, "rb");//只读 if (fp == NULL) { fprintf(stderr, "can't open file.txt\n"); return(EXIT_FAILURE); } /*char ch = 0; while (EOF != (ch = getc(fp))) { printf("%c", ch); } */ //统计字符数 count_ch(fp); //统计单词数 count_word(fp); //统计行数 count_lines(fp); //统计非空行数 count_non_empty_line(fp); fclose(fp); free(file); file = NULL; system("pause"); return(0);}
#ifndef COUNT_TEXT_H#define COUNT_TEXT_H#include 
int count_ch(FILE* fp);int count_word(FILE* fp);int count_lines(FILE* fp);int count_non_empty_line(FILE* fp);#endif
#include"count_text.h"#include 
#include
//#define SPACE ' '//#define NON ('\n'!= (ch = getc(fp)))&&('\r'!= (ch = getc(fp)))&& ('!' != (ch = getc(fp))) && ('\t' != (ch = getc(fp)))&& (',' != (ch = getc(fp))) && ('*' != (ch = getc(fp)))int count_ch(FILE* fp){ char ch = 0; int count_ch = 0; rewind(fp);//返回文件开始处 while (EOF != (ch = getc(fp))) { ++count_ch; } printf("文本中的字符数为:%d\n", count_ch); return count_ch;}int count_word(FILE* fp){ int count_word = 0; char ch = 0; int flag = 0; rewind(fp); ch = getc(fp); while (EOF != ch) { //如果不是空白字符,就是标志单词的开始 //if ((ch != ' ') && ('!' != ch) && ('*' != ch) && ('\r' != ch) && ('\n' != ch)) if (isalnum(ch)) { if (flag == 0) { flag = 1;//标记单词 } } //如果是空白字符,再次判断是不是单词状态(排除多个连续空白字符,误被统计成单词) else { if (flag == 1) { ++count_word; flag = 0; } } ch = getc(fp); } //判断最后一个非空白字符的单词 if (flag == 1) { ++count_word; } printf("the count of word is:%d\n", count_word); return count_word;}int count_lines(FILE* fp){ int count_line = 0; char ch = 0; rewind(fp); ch = getc(fp); while (EOF != ch) { if (ch == '\n') { ++count_line; } ch = getc(fp); } printf("the count of line:%d\n", count_line); return count_line;}int count_non_empty_line(FILE* fp){ int count_non_empty_line = 0; int count_empty_line = 0; char ch = 0; rewind(fp); while (EOF != ch) { if ((ch == '\n') && ((ch = getc(fp)) == '\r')) { ++count_empty_line; } ch = getc(fp); } int count=count_lines(fp); count_non_empty_line = count - count_empty_line; printf("the count of non_empty_line: %d\n", count_non_empty_line); return count_non_empty_line; }
a this is bookc standard   librarylibrary functions and macros  end

 

转载地址:http://ylypi.baihongyu.com/

你可能感兴趣的文章
RocketMQ-一文读懂架构、源码、调优与面试
查看>>
Kafka-一文读懂架构、源码、调优与面试
查看>>
Hadoop的部署与Minio区别
查看>>
在Alluxio上运行Apache Hive
查看>>
docker常用命令与部署方式
查看>>
基于kubeadm方式快速搭建K8s集群
查看>>
基于二进制方式搭建K8s集群-前置环境准备
查看>>
华为、阿里、京东使用的Java web框架是啥样的?
查看>>
1. Spring4.1-依赖注入
查看>>
2. Spring4.1-Java Config
查看>>
0. Spring4.1-环境搭建
查看>>
3.Spring MVC 4.1-@RequestMapping
查看>>
4. Spring MVC 4.1-REST
查看>>
5. Spring MVC 4.1-拦截器
查看>>
6. Spring MVC 4.1-服务器端推送
查看>>
7. Spring MVC 4.1-文件上传
查看>>
8. Spring MVC4.1-ContentNegotiatingViewResolver
查看>>
9. Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)
查看>>
10.Spring MVC4.1-异步请求处理(包含兼容浏览器的服务器端推送)
查看>>
11. Spring MVC4.1-全局异常处理
查看>>