On UNIX, to control the return of data from a terminal device, you use the tcsetattr () / tcgetattr () functions to change the characteristics of the terminal device. Apparently, these functions are also POSIX.2001.
C, , , (. , ):
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
int main(int argc, char * argv[]) {
int i[4] = {0};
struct termios oldtermios, newtermios;
tcgetattr(STDIN_FILENO, &oldtermios);
newtermios = oldtermios;
newtermios.c_lflag &= ~ICANON;
newtermios.c_cc[VMIN] = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &newtermios);
do {
printf("Enter four numbers:\n");
int ns = scanf(" %d %d %d %d", &i[0], &i[1], &i[2], &i[3]);
if(ns == EOF) {
perror("Scan failed. Exiting");
break;
}
else if(ns != 4) {
printf("\nScan failed. Only read %d numbers. Press enter to continue.\n", ns);
while((i[0] = getchar()) != '\n' && i[0] != EOF);
}
else {
printf("\nScanned %d numbers %d %d %d %d\n", ns, i[0], i[1], i[2], i[3]);
}
} while(1);
return 1;
}