printw() 不会打印 uint8_t 数组中的单个字符

printw() won't print a single character from a uint8_t array

这是我的 main() 代码:

#include <stdio.h>
#include <stdlib.h>
#ifdef __gnu_linux__
    #include <ncurses.h>
#endif
#ifdef _WIN32
    #include <curses.h>
#endif
#include "terminal_info.h"
#include "interface.h"
int main()
{
    initscr();
    setvbuf(stdout, NULL, _IONBF, 0);
    testForUI();
    readUIFile();
    continuouslyUpdateInfo();
    refresh();
    printDocument();
    getch();
    endwin();
    clearAll();
    return 0;
}

我遇到的问题发生在 printDocument():

void printDocument()
{
    int counter = 0;
    printw("Document Size: %d\n", documentSize);
    for(counter = 0; counter < documentSize; counter++)
    {
        printw("%c",(char)document[counter]);
        refresh();
    }

}

在for循环开始时,gdb说文档的内容如下:

 = (uint8_t *) 0x3e32c0 "<Root>\r\n    <Hello>World</Hello>\r\n    <This>\r\n
<Is>:-)</Is>\r\n        <An>:-O</An>\r\n        <Example>:-D</Example>\r\n    </This>\r\n</Root>\r\n««««««««_î_î_"

但程序只打印:

Document Size: 123

但是,如果我将 printw 语句更改为:

printw("%c - %d",(char)document[counter], document[counter]);

我明白了:

Document Size: 123

 - 13
 - 10  - 32  - 32  - 32  - 32< - 60H - 72e - 101l - 108l - 108o - 111> - 62W - 8
7o - 111r - 114l - 108d - 100< - 60/ - 47H - 72e - 101l - 108l - 108o - 111> - 6
 - 13
 - 13
 - 10    - 9< - 60I - 73s - 115> - 62: - 58- - 45) - 41< - 60/ - 47I - 73s - 115
 - 13
 - 10    - 9< - 60A - 65n - 110> - 62: - 58- - 45O - 79< - 60/ - 47A - 65n - 110
 - 13
 - 10    - 9< - 60E - 69x - 120a - 97m - 109p - 112l - 108e - 101> - 62: - 58- -
 - 13
 - 13
 - 13
 - 10

我尝试使用 setvbuf() 在 main() 中禁用缓冲,但它对我没有任何好处。

这是阅读和打印的文档,如果对您有帮助:

<Root>
    <Hello>World</Hello>
    <This>
    <Is>:-)</Is>
    <An>:-O</An>
    <Example>:-D</Example>
    </This>
</Root>

文档声明:extern uint8_t * document;

您需要删除回车 return (CR) 字符 (\r)。

当您输出 CR 时,ncurses 会将光标重置到同一行的第一列。然后当你输出一个 NL (\n) 时,ncurses 在将光标前进到下一行之前从光标位置擦除到行尾。这有效地删除了刚刚打印的整行。

这种行为是有记录的,这是值得的。来自 man waddch(强调已添加):

If ch is a tab, newline, or backspace, the cursor is moved appropriately within the window. Backspace moves the cursor one character left; at the left edge of a window it does nothing. Newline does a clrtoeol, then moves the cursor to the window left margin on the next line, scrolling the window if on the last line. Tabs are considered to be at every eighth column. The tab interval may be altered by setting the TABSIZE variable.

对回车的响应 return 记录在 ncurses waddch 联机帮助页中,在可移植性部分的末尾:

If ch is a carriage return, the cursor is moved to the beginning of the current row of the window. This is true of other implementations, but is not documented.

(感谢 Thomas Dickey 指向可移植性部分。)