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

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

最佳答案

因为"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/

相关文章:

unicode - ID3v2.3 标签中的文本编码

数字上方的 CSS 点

HTML 实体与。使用的 Unicode 代码点

php - 如何在 Utf-8 -> php 脚本 -> mysql 中执行 HTTP POST

c - C11 的最新变化

c++ - 缓存未命中压力测试 : stunning results. 。有什么解释吗?

c - 插入排序链表

c - printf 在 C 中用\r删除内容

c - printf C 错误

c++ - cout.rdbuf(fout.rdbuf()) 之后哪个语言环境将应用于 cout?