我有这个程序可以将文本文件的前 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);
最佳答案
我怀疑您正试图在窗口边界之外打印。
特别是,我猜这里:
mvwprintw(results_scrn,num_rows_res,(num_col_res/2) - 2,"Close");
...num_rows_res
是 results_scrn
窗口中的行数 - 但这意味着有效的行坐标范围从 0
到num_rows_res - 1
。
如果您尝试在窗口外move()
或wmove()
,光标实际上不会移动;随后的 printw()
或 wprintw()
将在前一个光标位置打印。如果您尝试 mvprintw()
或 mvwprintw()
,整个调用将在尝试移动光标时失败,因此它不会打印任何内容全部。
这是一个完整的演示(只是打印到 stdscr
,它有 LINES
行和 COLS
列):
#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
。)
关于c++ - 使用 curses 库时使用 move() 或 wmove() 时光标不会移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661491/