2011-10-05 25 views
2

我有這個程序,它將前5行或更多的文本文件打印到curses窗口,然後打印一些個性化輸入。但是在打印文本文件中的行後,遊標在使用move或wmove時不會移動。在使用both和refresh()後,我打印了一個單詞,但它打印在光標所在的最後一個位置。我嘗試了mvprintw和mvwprintw,但這樣我根本沒有輸出。 這是代碼當使用curses庫時使用move()或wmove()時,光標將不會移動

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

回答

2

我懷疑你正試圖將窗口的邊界之外打印的一部分。

特別,我猜這裏:

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

... num_rows_res是在results_scrn窗口的行數 - 但是,這意味着,有效行的座標範圍從0num_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在失敗的情況下)

相關問題