MINGW + notepad++
strlen遇到汉字的问题:
#include <iostream>
#include <cstring>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
汉字编码问题请看:
http://ir.hit.edu.cn/~taozi/bianma.htm