考虑以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#define MAX 64
void main(void)
{
char napis[] = "coś żółtego";
int i;
wchar_t bufor[MAX];
int ret;
setlocale(LC_CTYPE, "");
printf("zwykły string [%s] długość %d\n", napis, mblen(napis, MAX)); // <---
ret = mbstowcs(bufor, napis, sizeof bufor);
if (ret == MAX)
bufor[MAX-1] = '\0';
if (ret > 0)
{
printf("16 bitowy string: [%ls] długość napisu %d długość %d\n", bufor, mblen(napis, MAX), ret);
for(i = 0; i < ret; i++)
printf("%lc|", bufor[i]);
printf("\n");
}
}
当使用 gcc -o file file.c
运行代码时,我总是从 mblen()
得到 1。为什么会这样?
其他一切都完美无缺,但 mblen()
却不行。
我试过使用 LC_ALL,但没有任何改变。
最佳答案
因为"coś żółtego"
中第一个"c"
只有1个字节
the mblen() function inspects at most n bytes of the multibyte string starting at s and extracts the next complete multibyte character.
更改为“żoś żółtego”,看看会发生什么。
关于c - 为什么 mblen() 总是返回 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38158407/