繁体   English   中英

在 cmd 上打印到 stderr 无法打印非 ASCII UTF-8 文本的第一个字符

[英]Printing to stderr on cmd fails to print first character of non-ASCII UTF-8 text

这就是我的代码的样子:

#include <windows.h>
#include <stdio.h>

int main()
{
    SetConsoleCP(CP_UTF8);
    SetConsoleOutputCP(CP_UTF8);

    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX cfie;
    ZeroMemory(&cfie, sizeof(cfie));
    cfie.cbSize = sizeof(cfie);
    lstrcpyW(cfie.FaceName, L"Lucida Console");
    SetCurrentConsoleFontEx(hStdOut, 0, &cfie);

    fprintf(stderr, "нијe\n");
}

output 是这样的:

��ијe

当打印行替换为: printf("нијe\n");

output 是预期的:

нијe

我可能应该提到我用 MinGW-GCC 编译它并在 CMD 中运行它。

SetConsoleOutputCP()单独为我工作。 甚至“Consolas”作为字体也给出了正确的字符。

#include <windows.h>
#include <stdio.h>

int main()
{
    fprintf(stderr, "нијe\n");
    SetConsoleOutputCP(CP_UTF8);
    fprintf(stderr, "нијe\n");
}

我用MinGW-W64 的 GCC 8.1.0编译:

> gcc -Wall -Wextra -pedantic console.c -o console.exe

操作系统是 Windows 10 (1903),使用以下命令行在命令提示符下运行:

> for %i in (850 1252 65001) do ( mode con cp select=%i & console.exe & mode con /status )

它产生这个 output (德国语言环境,对不起):

> (mode con cp select=850   & console.exe   & mode con /status  )

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        850

ð¢ð©Ðÿe
нијe

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        850

> (mode con cp select=1252   & console.exe   & mode con /status  )

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        1252

нијe
нијe

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        1252

> (mode con cp select=65001   & console.exe   & mode con /status  )

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        65001

нијe
нијe

Status von Gerät CON:
---------------------
    Zeilen:          9999
    Spalten:         120
    Wiederholrate:   31
    Verzögerungszeit:1
    Codepage:        65001

请注意:如果您想要stderr的句柄,请使用STD_ERROR_HANDLE而不是STD_OUTPUT_HANDLE

暂无
暂无

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

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