开发者

cursor won't move when using move() or wmove() when using the curses library

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() 开发者_JS百科 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);


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜