统计ANSI格式下文件中中文个数(不包括中文运算符)

#include<iostream>
#include<fstream>
using namespace std;
// 关于ANSI,unicode与utf-8的区别:https://blog.csdn.net/forest_fire/article/details/81011090
//以下程序对gb2312编码有效,文档格式为ansi
void  calculate(const char* srcFile)
{
	unsigned char ch[2];
	ifstream inFile(srcFile, ios::in);
	if (!inFile)
	{
		cerr << "File could not be open." << endl;
		exit(-1);
	}
	int all_num = 0;
	string s = "";
	while (true)
	{
		inFile >> ch[0];
		if (inFile.eof())break;
		//GB2312编码范围:A1A1-FEFE,
		//其中汉字的编码范围为B0A1-F7FE,第一字节0xB0-0xF7(对应区号:16-87),第二个字节0xA0-0xFE(对应位号:01-94)。
		if (ch[0] >= 0xb0 && ch[0] <= 0xf7)//GB2312下的汉字,最小是0XB0 
		{
			inFile >> ch[1];
			if (ch[1] >= 0xa0 && ch[1] <= 0xfe)
				all_num += 1;
		}
		else
			continue;
	}
	cout << "总中文数" << all_num << endl;
	inFile.close();
}
int main()
{
  calculate("C:\\Users\\Lenovo\\Desktop\\1.txt");
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值