我正在为一个更大的程序做一些测试,这里我试图在一个充满“*”的字符数组中移动一个空白,它工作正常,但是在发送许多键后左右键的值发生了变化,我不明白为什么
这是我的代码:
include <curses.h>
//#include <term.h>
#include <termios.h>
#include <stdlib.h>
#include "includes/my_list.h"
void init_term()
{
struct termios term;
if (tcgetattr(0, &term) != 0)
{
printf("Fail to get input termios\n");
// return(0);
}
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &term) != 0)
{
printf("Fail to set input termios\n");
// return(0);
}
}
t_info change_dir(t_info info, int flag)
{
if(flag == 0)
info.pos++;
else if (flag == 1)
info.pos--;
info.tab[info.pos] = ' ';
return(info);
}
int main()
{
int c;
int i; int pos;
t_info info;
// tgetent(NULL, "xterm");
info.tab = malloc(sizeof(*info.tab) * 10);
i = 0;
info.pos = 0;
init_term();
while (read(0, &c, sizeof(int)) != 0)
{
printf("c = %d\n", c);
// sleep(1);
while(i < 9)
{
info.tab[i] = '*';
i++;
}
i = 0;
if(c == 4479771)
{
printf("left ok1\n");
info = change_dir(info, 1);
}
else if (c == 4414235)
info = change_dir(info, 0);
printf("%s\n", info.tab);
}
}