首页 文章

无法从NCURSES中的stdin扩展ASCII字符读取

提问于
浏览
1

我试图在NCURSES中读取扩展的ASCII字符时遇到问题 .

我有这个程序:

#include <ncurses.h>
int main () {
    initscr();
    int d = getch();
    mvprintw(0, 0, "letter: %c.", d);
    refresh();
    getch();
    endwin();
    return 0;
}

我用它构建它:gcc -lncursesw a.c

如果我在7位ascii中键入一个字符,就像'e'字符,我得到:

letter: e.

然后我必须输入另一个程序来结束 .

如果我在扩展的ascii中输入一个字符,比如'á'字符,我得到:

letter:  .

程序结束 .

它就像第二个字节被读作另一个字符 .

我怎样才能得到正确的char'á'???

谢谢!

1 回答

  • 1

    要键入的字符需要程序来设置区域设置 . 如_775969中所述:

    Initialization
    
       The  library uses the locale which the calling program has
       initialized.  That is normally done with setlocale:
    
             setlocale(LC_ALL, "");
    
       If the locale is not initialized, the library assumes that
       characters  are  printable  as in ISO-8859-1, to work with
       certain legacy programs.  You should initialize the locale
       and  not  rely on specific details of the library when the
       locale has not been setup.
    

    过去,您的语言环境可能使用UTF-8 . 要使用UTF-8,您应该编译并链接到ncursesw库 .

    此外, getch 函数仅返回单字节编码的值,例如ISO-8859-1,有些人与Windows cp1252混淆,从而"Extended ASCII"(其中有两个谬误没有取消) . UTF-8是一种多字节编码 . 如果您使用 getch 来读取它,您将获得该字符的第一个字节 .

    相反,要读取UTF-8,您应该使用get_wch(除非您想自己解码UTF-8) . 这是一个修订的程序,它可以:

    #include <ncurses.h>
    #include <locale.h>
    #include <wchar.h>
    int
    main(void)
    {   
        wint_t value;
        setlocale(LC_ALL, "");
        initscr();
        get_wch(&value);
        mvprintw(0, 0, "letter: %#x.", value);
        refresh();
        getch();
        endwin();
        return 0;
    }
    

    我将结果打印为数字,因为printw不知道Unicode值 . printw 使用与 printf 相同的C运行时支持,因此您可以直接打印该值 . 例如,我看到POSIX printf有一个用于处理 wint_t 的格式化选项:

    c int参数应转换为unsigned char,并写入结果字节 . 如果存在l(ell)限定符,则wint_t参数应该转换为ls转换规范,没有精度,并且参数指向wchar_t类型的双元素数组,其第一个元素包含wint_t参数到ls转换规范,第二个元素包含一个空宽字符 .

    由于ncurses可以在许多平台上运行,因此并非所有平台都支持该功能 . 但是你可以假设它适用于GNU C库:大多数发行版通常提供可行的语言环境配置 .

    这样做,这个例子更有趣:

    #include <ncurses.h>
    #include <locale.h>
    #include <wchar.h>
    int
    main(void)
    {   
        wint_t value;
        setlocale(LC_ALL, "");
        initscr();
        get_wch(&value);
        mvprintw(0, 0, "letter: %#x (%lc).", value, value);
        refresh();
        getch();
        endwin();
        return 0;
    }
    

相关问题