我一直在用C玩ncurses,并且一直在努力使这小段代码正常工作一个小时。
我尝试打印以下形式的3x3字段:(仅以3x3代替2x2)
00000
0 0 0
00000
0 0 0
00000
我认为我的代码运行正常,没有警告或错误出现,但是它没有显示00000
,而是显示了0
。
即当我将代码更改为显示abc
时,它会显示出来。
代码:
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <ncurses.h>
void drawfield(uint16_t size, char c);
void quit();
int main(int argv, char **argc) {
//read in the man page, that these settings should be good
initscr();
cbreak();
noecho();
curs_set(0);
atexit(quit);
drawfield(20, 'b');
refresh();
getch();
return 0;
}
void drawfield(uint16_t size, char c) {
//Preparing strings
char *line = (char *)malloc(sizeof(char) * (size + 1));
char *rest = (char *)malloc(sizeof(char) * (size + 1));
// line should be of format "ccccccccc", while rest should be "c c c"
for (int i=0; i<size; i++) {
// if i use c+i instead of c it seems to work fine
*(line+i) = c;
*(rest+i) = (i % (size/3))?' ':c;
}
line[size] = '\0';
rest[size] = '\0';
// debug output, strings seem to be correct
printf("line: %s \n rest: %s \n", line, rest);
sleep(3);
//Drawing part
//Should be drawing a 3x3 field
for (int i=0; i<size; i++) {
if (i % (size/3) == 0) {
mvaddstr(i, 0, line);
} else {
mvaddstr(i, 0, rest);
}
}
}
void quit() {
endwin();
}
输出: (相同的字符方法):
b
b b b b
b b b b
b b b b
b b b b
b b b b
b
b b b b
b b b b
b b b b
b b b b
b b b b
b
b b b b
b b b b
b b b b
b b b b
b b b b
b
b b b b
(更改字符)
bcdefghijklmnopqrstu
b b b b
b b b b
b b b b
b b b b
b b b b
bcdefghijklmnopqrstu
b b b b
b b b b
b b b b
b b b b
b b b b
bcdefghijklmnopqrstu
b b b b
b b b b
b b b b
b b b b
b b b b
bcdefghijklmnopqrstu
b b b b
我也直接尝试使用addch方法而不是addstr,但这似乎没有任何作用。
如果您知道为什么会这样和/或如何解决它,我真的很想知道! 预先感谢。
编辑:
我使用ArcoLinux作为我的发行版,并使用-lncurses
标志
编辑2:
问题是我使用的是urxvt,但已将TERM变量设置为xterm-256color
而不是rxvt-256color
。通过我的.Xresources文件termName
条目对此进行了修复。