Code
![]()
#include <wchar.h>
#include <stdio.h>
#include <string.h>
![]()
int main()
![]()
![]()
{
int count=0;
wchar_t buff[200];
wchar_t *temp=L"我的太阳呀";
![]()
fgetws(buff,200,stdin);
count=wcslen(buff);
printf("%d,%d",count,wcslen(temp));
![]()
fgetc(stdin);
}
上边这段代码在输入“
我的太阳呀”后,输出的结果是11,5,这让我很奇怪,按照wcslen的定义我们可以知道,返回的应该是6呀。从网上查资料后知道,用下面的代码可以实现对汉字的长度处理。
![]()
Code
1
#include <wchar.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <locale.h>
5![]()
6
int main()
7![]()
![]()
{
8
int count=0;
9
wchar_t buff[200];
10
wchar_t *temp=L"我的太阳呀";
11
12
setlocale( LC_ALL, "" );
13
14
fgetws(buff,200,stdin);
15
count=wcslen(buff);
16
printf("%d,%d",count,wcslen(temp));
17![]()
18
setlocale( LC_ALL, "C" );
19
fgetc(stdin);
20
}
这个的输出结果是6,5,之所以 输出 是6 ,是因为fgetws和fgets函数在读入字符串时,会读入回车字符,所以就多一个字符。