ncurses inchstr功能不按预期工作?

时间:2016-05-11 20:42:07

标签: c++11 ncurses curses

#include <curses.h>
#include <fstream>

int main(){

    initscr();
    mvwprintw(stdscr, 1,1, "hello world");
    wmove(stdscr, 1,1);

    chtype* p = 0;
    int n = 0;
    n = winchstr(stdscr, p);

    std::ofstream test("test.txt");

    test << n << " " << (p == nullptr);

    endwin();

}

在上面我打印hello world,将光标移回到句子的开头,并尝试使用p将屏幕上内容的内容存储到winchstr。除了文件test.txt之外,我只看到输出-1 1建议p未更改且返回ERR(即-1),因为documentation描述了:

  

int winchstr(WINDOW * win,chtype * ch);

     

描述:       这些例程从窗口读取chtype或cchar_t字符串,       从当前或指定的位置开始,到结尾处       右边距,或在n个元素之后,以较小者为准。

     

返回值:       所有函数都返回读取的元素数或ERR       错误。

我误解了文档吗?从hello world开始的行的内容应该在使用p之后存储在winchstr指向的位置吗?为什么会返回错误?

1 个答案:

答案 0 :(得分:1)

位置p是空指针; ncurses可能会看到,并拒绝使用它。手册页说

  

未定义错误条件。如果 chstr 参数   为null,不返回任何数据,返回值为零。

如果使用winchstr,使用非空指针,ncurses将(必须)假设您为结果提供了足够的空间。使用winchnstr指定大小。