I'm trying to write a "more"(just like "more" instruction in Linux) program in C. Quit when "q" is pressed and show one more page when " " is pressed. But everytime I have to press Enter after the command. I tried to use setbuf() and setvbuf() but it doesn't work.Why?How should I do?
FILE *fp_tty; //read from keyboard
fp_tty = fopen("/dev/tty", "r");
setbuf(fp_tty, NULL);
see_more(fp_tty);
......
int see_more(FILE *cmd) {
int c;
printf("\033[7m more? \033[m");
while ((c = getc(cmd)) != EOF) {
if (c == 'q') {
return 0; //quit
}
if (c == ' ') {
return PAGELEN; //next page
}
if (c == '\n') {
return 1; //1 line
}
}
return 0;
}