php获取汉字个数,获取文件中汉字个数

MINGW + notepad++

strlen遇到汉字的问题:

#include

#include

using namespace std;

int main()

{

cout << strlen("汉字") << endl;

}

以UTF-8无BOM编码输出结果为:6

以ANSI编码为:4

可见这个问题的答案与采用的字符编码方式有关。

对于GB2312:

汉字的第一字节:是从0xB0   开始编码  0xB0-0xF7(176-247)

汉字的第二字节:是从0xA0   开始编码  0xA0-0xFE(160-254)

#include

#include

#include

int main(int argc, char *argv[])

{

int ch;

int count = 0;

FILE *fstream;

if (argc < 2)

{

printf("Input Error!\nUsage:programmename filename\n");

printf("输入错误!\n用法:程序名 文件名\n");

return -2;

}

if ((fstream = fopen(argv[1], "r")) == NULL)

{

printf("File open error!\n");

printf("文件打开出错!\n");

return -1;

}

while (!feof(fstream))

{

ch = getc(fstream);

if (ch >= 0xB0)

{

ch = getc(fstream);

if (ch >= 0XA0)

{

count++;

}

}

}

printf("%s 包含%d个汉字\n", argv[1], count);

return 0;

}

汉字编码问题请看:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值