I have been playing around with ncurses in C a bit, and have been trying to get this little piece of code working correctly for an hour now.
I try to print a 3x3 field of the form: (just in 3x3 instead of 2x2)
00000
0 0 0
00000
0 0 0
00000
And I think my code is working correctly, no warnings or errors are appearing, but instead of 00000 it just displays 0.
I.e. when I change my code to display abc it does display it.
Code:
#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();
}
Output: (the same character method):
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
(changing the character)
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
I also directly tried using the addch method instead of addstr, but it doesn't seem to make any difference.
If you know why this happens and/or how to work around it, I would really like to know! Thanks in advance.
EDIT:
I'm using ArcoLinux as my distro and gcc as the compiler using the -lncurses flag
EDIT 2:
The problem was that I was using urxvt, but had set the TERM variable to xterm-256color instead of rxvt-256color. Changing this via my .Xresources files termName entry fixed it.