1

我使用 Xshell 连接到远程主机并编写 ncurse 程序。但是发现不能向右循环打印字符。

#include <ncurses.h>
int main() {
    initscr();
    for (int i = 0;i < 10; i++){
        addch('o');
    }
    refresh();
    getch();
    endwin();
    return 0;
}

结果: 只打印第一个字符。

为了打印出来,我不得不添加刷新代码

for (int i = 0; i < 10;i++) {
    addch('o');
    refresh();//print all 'o'
}

我认为BIG问题导致box功能不可用,因为一个box的两条水平线没有完成,它也只有第一个'-'。

int main() {
    initscr();
    curs_set(0);
    WINDOW *win;
    win = newwin(10, 40, 9, 20);
    box(win, 0, 0);
    refresh();
    wrefresh(win);
    getch();
    endwin();
    return 0;
}

结果: 注意上下两条水平线

我无法弄清楚这个问题。

2021.3.11
我有我已经发现问题了。问题出在 Xshell 上。我通过阿里云服务器官网连接我的主机,程序没有问题。但我仍然不知道如何设置我的 xshell。

4

1 回答 1

1

使用下面的代码(保存test.c并编译gcc -o test test.c -lncurses)我可以画一个边框,把文本放在里面,也可以把文本放在主窗口中。

#include <ncurses.h>

// Print the character 'ch' in the window
// 'win', and do this 'repeat' times.
int print_chars(WINDOW *win, char ch, int repeat) {
    if (win == NULL) {
    // Print to main window
        for (int i=0; i<repeat; i++) {
            addch(ch);  
        refresh();
        }
    } else {
    // Print to the named window
        for (int i=0; i<repeat; i++) {
            waddch(win, ch);    
        }
        wrefresh(win);
    }
    return 0;
}

int main(int argc, char **argv) 
{
    WINDOW *border_win, *win;
    
    initscr();
    refresh(); // This seems to be needed; 
               // I don't know why.
    
    // Create a window to show the border
    border_win = newwin(10,40,9,20);
    box(border_win,0,0);
    wrefresh(border_win);
    
    // Create a window inside that, which 
    // we will write in.  Otherwise it 
    // seems we will overwrite the border.
    win = newwin(8,38,10,21);
    print_chars(win, 'a', 10);

    // Write something in the main window
    // as well, just to show it works
    print_chars(NULL, 'o', 10);
    
    getch();
    endwin();
    return 0;
}
于 2021-03-10T09:07:01.137 回答