简体   繁体   English

为什么mblen()总是返回1?

[英]Why mblen() always returns 1?

Consider following code: 考虑以下代码:

#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");
    }
}

When the code with gcc -o file file.c and run it, I always get 1 from mblen() . 当代码与gcc -o file file.c运行时,我总是从mblen()获得1。 Why does it happen? 为什么会发生?

Everything else works perfectly, but mblen() does not. 其他所有内容都可以正常运行,但是mblen()不能。

I've tried to use LC_ALL, but nothing changed. 我尝试使用LC_ALL,但没有任何更改。

Because the first "c" in "coś żółtego" is only 1 byte 因为"coś żółtego" "c"中的第一个"c"只有1个字节

the mblen() function inspects at most n bytes of the multibyte string starting at s and extracts the next complete multibyte character . mblen()函数检查从s开始的多字节字符串中最多n个字节,并提取下一个完整的多字节字符

Change to "żoś żółtego" and see what happens. 更改为“żośżółtego”,然后看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM