【问题标题】:ncurses inchstr function not working as intended?ncurses inchstr 函数没有按预期工作?
【发布时间】:2016-05-11 20:42:06
【问题描述】:
#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,将光标移回句子的开头,并尝试使用winchstr将屏幕上的内容存储到p。除了在文件test.txt 中,我只看到输出-1 1 暗示p 没有改变并且ERR(即-1)被返回,正如documentation 所描述的那样:

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

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

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

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

【问题讨论】:

    标签: c++11 ncurses curses


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。我假设它通过引用获取指针并将指针位置更改为指向我想要的位置。 (我知道它并没有说它通过引用来获取它,但是 curses 的 getyx 函数也没有改变两个传递的整数)。
    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2016-10-10
    • 2016-04-08
    相关资源
    最近更新 更多