简体   繁体   English

无法在NCURSES中正确打印扩展的ASCII字符

[英]Cannot correctly print extended ASCII characters in NCURSES

I have this program: 我有这个程序:

#include <ncurses.h>
int main () {
    initscr();
    mvaddstr(0, 0, "              A         B         C         D         E       ");
    mvaddstr(1, 24, "ñandñ");
    mvaddstr(1, 34, "esdrñjulñ");
    refresh();
    getch();
    endwin();
    return 0;
}

When ncurses prints the first word (ñandñ) something happens that when moving to another position afterwards (in this case to 1, 34), actually it moves to another position, so the second word gets printed in another column. 当ncurses打印出第一个单词(ñandñ)时会发生一些事情,当之后移动到另一个位置时(在这种情况下为1,34),实际上它会移动到另一个位置,所以第二个单词会在另一个列中打印出来。

So what it should look like: 那应该是什么样子:

          A         B         C         D         E       
                              ñandñ     esdrñjulñ

looks like this: 看起来像这样:

          A         B         C         D         E       
                              ñandñ   esdrñjulñ

because of the two 'ñ' extended ascii char in the first word. 因为第一个单词中有两个'ñ'扩展的ascii字符。

Any idea what is wrong? 知道什么是错的吗? Thanks! 谢谢!

If you want to use multibyte UTF-8 characters, you must use the version of the ncurses library compiled with multibyte support, and you need to set the locale correctly at the beginning of your program. 如果要使用多字节UTF-8字符,则必须使用以多字节支持编译的ncurses库版本,并且需要在程序开头正确设置区域设置。

The multibyte ncurses library is usually called libncursesw , so to use it is is sufficient to change -lncurses to -lncursesw in your linker options. 多字节ncurses库通常被称为libncursesw ,这样使用起来就足以改变-lncurses-lncursesw在你的连接选项。 You do not need a different header file. 您不需要不同的头文件。 However, if you actually want to use the wide-character functions, you must #define the symbol _XOPEN_SOURCE_EXTENDED before any #include directive. 但是,如果您确实想要使用宽字符函数,则必须在任何#include指令之前#define符号_XOPEN_SOURCE_EXTENDED The easiest way to do that with gcc (or clang) is to add -D_XOPEN_SOURCE_EXTENDED to your compiler options. 使用gcc(或clang)执行此操作的最简单方法是将-D_XOPEN_SOURCE_EXTENDED添加到编译器选项中。

If your shell's locale has been set to a UTF-8 locale (which will usually be the case on a modern Linux distribution), it is sufficient to insert 如果shell的语言环境已设置为UTF-8语言环境(在现代Linux发行版中通常就是这种情况),则插入就足够了

setlocale(LC_ALL, "");

before any call to an ncurses routine. 在任何调用ncurses例程之前。 That sets the executable's locale to the locale configured by the environment variables. 这将可执行文件的区域设置设置为由环境变量配置的区域设置。 You'll need to add 你需要添加

#include <locale.h>

to your header includes. 你的标题包括。 No special library needs to be linked for locale support. 不需要链接特殊库来支持语言环境。


The original question indicated that mvaddwstr was being used. 最初的问题表明正在使用mvaddwstr That's generally a better solution for multibyte characters, but as indicated above, you can use the narrow string interfaces if you want to. 这通常是多字节字符的更好解决方案,但如上所述,您可以根据需要使用窄字符串接口。 However, you cannot output incomplete UTF-8 sequences, so the single-character narrow interfaces like addch can only be used with character codes less than 128. 但是,您不能输出不完整的UTF-8序列,因此像addch这样的单字符窄接口只能用于小于128的字符代码。

This note applied to an attempt to call mvaddwstr with a char* instead of a wchar_t* argument. 本说明适用于尝试使用char*而不是wchar_t*参数调用mvaddwstr Don't do that: 不要这样做:

Like all of the w ncurses functions which accept strings, mvaddwstr takes a wchar_t* as its string argument. 像所有接受字符串的w ncurses函数一样, mvaddwstrwchar_t*作为其字符串参数。 Your compiler should have warned you about that (unless it warned you that there was no prototype for mvaddwstr ). 你的编译器应该警告过你(除非它警告你没有mvaddwstr原型)。 So the strings should be prefixed with the L length attribute: L"ñandñ" . 所以字符串应该以L length属性作为前缀: L"ñandñ"

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

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