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

时间:2016-07-02 09:04:39

标签: c unicode

请考虑以下代码:

#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,但没有任何改变。

1 个答案:

答案 0 :(得分:2)

因为default: &default adapter: postgresql encoding: UTF-8 pool: 5 development: <<: *default database: myApp_development username: deployer password: password test: <<: *default database: myApp_test username: deployer password: password production: <<: *default database: myApp_production username: deployer password: password 中的第一个"c"只有1个字节

  

mblen()函数检查多字节字符串的最多n个字节   从s开始并提取下一个完整的多字节字符

更改为“żośżółtego”,看看会发生什么。

相关问题