我想创建一个程序,强制它的用户输入文本,但不允许他删除任何一个,在C中这样做的简单方法是什么?
我唯一得到的是(c = getchar()) != EOF && c != '\b'哪些不起作用.有任何想法吗?
POSIX - unix版本
#include <sys/types.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
int
main()
{
int fd=fileno(stdin);
struct termios oldtio,newtio;
tcgetattr(fd,&oldtio); /* save current settings */
memcpy(&newtio, &oldtio, sizeof(oldtio));
newtio.c_lflag = ICANON;
newtio.c_cc[VERASE] = 0; /* turn off del */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
/* process user input here */
tcsetattr(fd,TCSANOW,&oldtio); /* restore setting */
return 0;
}
Run Code Online (Sandbox Code Playgroud)