使用ncurse库在for循环中向右打印字符的问题
我使用Xshell连接到远程主机并编写ncurse程序。但人们发现,这些字符无法在一个循环中打印到右边使用ncurse库在for循环中向右打印字符的问题,c,ncurses,remote-host,C,Ncurses,Remote Host,我使用Xshell连接到远程主机并编写ncurse程序。但人们发现,这些字符无法在一个循环中打印到右边 #include <ncurses.h> int main() { initscr(); for (int i = 0;i < 10; i++){ addch('o'); } refresh(); getch(); endwin(); return 0; } 结果: 我想不出这个问题 2021.3.1
#include <ncurses.h>
int main() {
initscr();
for (int i = 0;i < 10; i++){
addch('o');
}
refresh();
getch();
endwin();
return 0;
}
结果:
我想不出这个问题
2021.3.11我已经确定了问题所在。问题在于Xshell。我通过阿里云服务器官网连接主机,程序没有问题。但是我仍然不知道如何设置xshell。使用以下代码(保存到
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;
}
#包括
//在窗口中打印字符“ch”
//“赢”,重复几次。
整数打印字符(窗口*win,字符ch,整数重复){
if(win==NULL){
//打印到主窗口
对于(int i=0;i请只描述一个问题,最好用a来演示。询问两个问题会让人困惑,使问题无法集中。请不要添加显示纯文本的图片。只需将文本复制并粘贴到问题中即可。您可以使用问题下方的编辑
按钮来解决此问题。您声称只有第一个字符被打印。这不是真的。您的代码打印o
(小写字母o),而您的图片包含0
(数字零)。这里有些不匹配。这是两个问题(关闭第一个,因为示例与图片不匹配;第二个是重复的,因为来自getch
的刷新会干扰wrefresh
——再次,与图片不匹配)。您用另一张图片替换了您的图片。请避免使用文本更合适的图片。问题很可能不在OP的程序中,而是在OP的环境中。
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;
}
#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;
}