[英]The problem of printing characters to the right in a for loop with the ncurse library
I use Xshell to connect to a remote host and write ncurse programs.我使用 Xshell 连接到远程主机并编写 ncurse 程序。 But it was found that the characters could not be printed in a cycle to the right.但是发现不能向右循环打印字符。
#include <ncurses.h>
int main() {
initscr();
for (int i = 0;i < 10; i++){
addch('o');
}
refresh();
getch();
endwin();
return 0;
}
Result: Only the first character is printed.结果:只打印第一个字符。
In order to print it all out, I had to add the refresh code为了打印出来,我不得不添加刷新代码
for (int i = 0; i < 10;i++) {
addch('o');
refresh();//print all 'o'
}
I think the BIG problem makes the box function unavailable, because the two Horizontal line of a box are not completed, it only have the first '-' too.我认为BIG问题使盒子function不可用,因为盒子的两条水平线没有完成,它也只有第一个'-'。
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;
}
Result: Notice the two horizontal lines up and down结果:注意上下两条水平线
I can't figure out this problem.我无法弄清楚这个问题。
2021.3.11 2021.3.11
I have I have identified the problem.我已经确定了问题所在。 The problem is with Xshell.问题出在 Xshell 上。 I connect my host by Alibaba Cloud Server official website, and the program have no problem.我通过阿里云服务器官网连接我的主机,程序没有问题。 But I still don't konw how to set up my xshell.但我仍然不知道如何设置我的 xshell。
Using the following code (save to test.c
and compile with gcc -o test test.c -lncurses
) I can draw a border, put text inside it, and also put text in the main window. Using the following code (save to test.c
and compile with gcc -o test test.c -lncurses
) I can draw a border, put text inside it, and also put text in the main window.
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.