使用curses库时使用move()或wmove()时光标不会移动

发布于 2024-12-08 10:38:35 字数 417 浏览 0 评论 0原文

我有一个程序,它将文本文件的前 5 行或更多行打印到curses 窗口,然后打印一些个性化输入。但是从文本文件打印行后,使用 move 或 wmove 时光标不会移动。我在使用和refresh()后打印了一个单词,但它打印在光标所在的最后位置。我尝试了mvprintw和mvwprintw,但这样我根本没有输出。 这是代码的一部分

while (! feof(results_file))
    {
        fgets(line,2048,results_file);
        printw("%s",line);
    }
fclose(results_file);
mvwprintw(results_scrn,num_rows_res,(num_col_res/2) - 2,"Close");
wrefresh(results_scrn);

I have this program which prints the first 5 lines or more of a text file to a curses window and then prints some personalized input. but after printing the lines from the text file, the cursor wont' move when using move or wmove. I printed a word after using both and refresh() but it is printed in the last position the cursor was in. I tried mvprintw and mvwprintw but that way I got no output at all.
this is a part of the code

while (! feof(results_file))
    {
        fgets(line,2048,results_file);
        printw("%s",line);
    }
fclose(results_file);
mvwprintw(results_scrn,num_rows_res,(num_col_res/2) - 2,"Close");
wrefresh(results_scrn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-12-15 10:38:35

我怀疑您正在尝试在窗口范围之外打印。

特别是,我猜测这里:

mvwprintw(results_scrn,num_rows_res,(num_col_res/2) - 2,"Close");

...num_rows_resresults_scrn 窗口中的行数 - 但这意味着有效的行坐标范围为 0num_rows_res - 1

如果您尝试在窗口外使用 move()wmove(),光标实际上不会移动;后续的 printw()wprintw() 将在上一个光标位置打印。如果您尝试 mvprintw()mvwprintw(),整个调用将在尝试移动光标时失败,因此不会打印任何内容全部。

这是一个完整的演示(仅打印到具有 LINES 行和 COLS 列的 stdscr):(

#include <stdio.h>
#include <curses.h>

int main(void)
{
    int ch;

    initscr();
    noecho();
    cbreak();

    /* This succeeds: */
    mvprintw(1, 1, ">>>");

    /* This tries to move outside the window, and fails before printing: */
    mvprintw(LINES, COLS / 2, "doesn't print at all");

    /* This tries to move outside the window, and fails: */
    move(LINES, COLS / 2);

    /* This prints at the cursor (which hasn't successfully moved yet): */
    printw("prints at current cursor");

    /* This is inside the window, and works: */
    mvprintw(LINES - 1, COLS / 2, "prints at bottom of screen");

    refresh();
    ch = getch();
    endwin();
    return 0;
}

事实上,函数确实返回结果;如果你检查一下,你会发现失败的情况下是ERR。)

I suspect that you are trying to print outside the bounds of the window.

In particular, I would guess that here:

mvwprintw(results_scrn,num_rows_res,(num_col_res/2) - 2,"Close");

...num_rows_res is the number of rows in the results_scrn window - but that means that the valid row coordinates range from 0 to num_rows_res - 1.

If you try to move() or wmove() outside the window, the cursor will not actually move; a subsequent printw() or wprintw() will print at the previous cursor position. If you try to mvprintw() or mvwprintw(), the whole call will fail at the point of trying to move the cursor, and so it won't print anything at all.

Here's a complete demonstration (just printing to stdscr which has LINES rows and COLS columns):

#include <stdio.h>
#include <curses.h>

int main(void)
{
    int ch;

    initscr();
    noecho();
    cbreak();

    /* This succeeds: */
    mvprintw(1, 1, ">>>");

    /* This tries to move outside the window, and fails before printing: */
    mvprintw(LINES, COLS / 2, "doesn't print at all");

    /* This tries to move outside the window, and fails: */
    move(LINES, COLS / 2);

    /* This prints at the cursor (which hasn't successfully moved yet): */
    printw("prints at current cursor");

    /* This is inside the window, and works: */
    mvprintw(LINES - 1, COLS / 2, "prints at bottom of screen");

    refresh();
    ch = getch();
    endwin();
    return 0;
}

(In fact the functions do return a result; if you check it, you'll find that it's ERR in the cases that fail.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文