当前位置: 技术问答>毕业设计课题求助:在linux平台下开发,有idea者请进!!!
iis7站长之家
毕业设计快完成了,还有最后几个小毛病………快救命…………之一
来源: 互联网 发布时间:2015-07-01
本文导语: 在字符界面下写了个登录程序,但是发现linux下没有getche()和getch(),后来在外国人的帖子里找到一个模拟的方法,代码如下: char getch() { int rv; char ch; int fd = 0; // force to stdin struct termios oldflags, newflags; /****...
在字符界面下写了个登录程序,但是发现linux下没有getche()和getch(),后来在外国人的帖子里找到一个模拟的方法,代码如下:
char getch()
{
int rv;
char ch;
int fd = 0; // force to stdin
struct termios oldflags, newflags;
/*********** Reset the terminal to accept unbuffered input ***/
/* get the current oldflags */
tcgetattr(fd, &oldflags);
/* make a copy of the flags so we can easily restore them */
newflags = oldflags;
/* set raw input */
newflags.c_cflag |= (CLOCAL | CREAD);
newflags.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* set the newflags */
tcsetattr(fd, TCSANOW, &newflags);
rv = read(fd, &ch, 1);
/* restore the oldflags -- This is important otherwise
* your terminal will no longer echo characters
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &oldflags);
return ch;
}
char getche()
{
int rv;
char ch;
int fd = 0; // force to stdin
struct termios oldflags, newflags;
/*********** Reset the terminal to accept unbuffered input ***/
/* get the current oldflags */
tcgetattr(fd, &oldflags);
/* make a copy of the flags so we can easily restore them */
newflags = oldflags;
/* set raw input */
newflags.c_cflag |= (CLOCAL | CREAD);
newflags.c_lflag &= ~(ICANON | ECHOE | ISIG);
/* set the newflags */
tcsetattr(fd, TCSANOW, &newflags);
rv = read(fd, &ch, 1);
/* restore the oldflags -- This is important otherwise
* your terminal will no longer echo characters
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &oldflags);
return ch;
}
问题是,getche()和getch()都会得到键盘缓冲区里的第一个字符,但是其余的字符得不到,就是说如果在程序执行到getche()和getch()之前输入了一串字符,例如“12345”,结果当执行到getche()或getch()的时候,“1”被接收下来,以后无论执行getche()和getch()多少次,后面的“2345”则永远不会得到。
我得问题是,有没有什么方法:要么全都能接受到,要么能清空缓冲区,我试过fflush(stdin)不过不管用,要么提供另一种在字符界面下登录程序输入部分的设计方法
另外,
char getch()
{
int rv;
char ch;
int fd = 0; // force to stdin
struct termios oldflags, newflags;
/*********** Reset the terminal to accept unbuffered input ***/
/* get the current oldflags */
tcgetattr(fd, &oldflags);
/* make a copy of the flags so we can easily restore them */
newflags = oldflags;
/* set raw input */
newflags.c_cflag |= (CLOCAL | CREAD);
newflags.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* set the newflags */
tcsetattr(fd, TCSANOW, &newflags);
rv = read(fd, &ch, 1);
/* restore the oldflags -- This is important otherwise
* your terminal will no longer echo characters
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &oldflags);
return ch;
}
char getche()
{
int rv;
char ch;
int fd = 0; // force to stdin
struct termios oldflags, newflags;
/*********** Reset the terminal to accept unbuffered input ***/
/* get the current oldflags */
tcgetattr(fd, &oldflags);
/* make a copy of the flags so we can easily restore them */
newflags = oldflags;
/* set raw input */
newflags.c_cflag |= (CLOCAL | CREAD);
newflags.c_lflag &= ~(ICANON | ECHOE | ISIG);
/* set the newflags */
tcsetattr(fd, TCSANOW, &newflags);
rv = read(fd, &ch, 1);
/* restore the oldflags -- This is important otherwise
* your terminal will no longer echo characters
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &oldflags);
return ch;
}
问题是,getche()和getch()都会得到键盘缓冲区里的第一个字符,但是其余的字符得不到,就是说如果在程序执行到getche()和getch()之前输入了一串字符,例如“12345”,结果当执行到getche()或getch()的时候,“1”被接收下来,以后无论执行getche()和getch()多少次,后面的“2345”则永远不会得到。
我得问题是,有没有什么方法:要么全都能接受到,要么能清空缓冲区,我试过fflush(stdin)不过不管用,要么提供另一种在字符界面下登录程序输入部分的设计方法
另外,
|
#ifdef TERMIOS
struct termios oldtty, noecho;
#else
#ifdef SGTTYB
struct sgttyb oldtty, noecho;
#else
struct termio oldtty, noecho;
#endif
/*
* EchoOff
* turn off echoing. we call this function when we want to read
* the password. (only for UNIX)
*/
void EchoOff()
{
int fd = fileno(stdin);
/*
* first, get the current settings.
* then turn off the ECHO flag.
*/
#ifdef TERMIOS
if (tcgetattr(fd, &oldtty)
struct termios oldtty, noecho;
#else
#ifdef SGTTYB
struct sgttyb oldtty, noecho;
#else
struct termio oldtty, noecho;
#endif
/*
* EchoOff
* turn off echoing. we call this function when we want to read
* the password. (only for UNIX)
*/
void EchoOff()
{
int fd = fileno(stdin);
/*
* first, get the current settings.
* then turn off the ECHO flag.
*/
#ifdef TERMIOS
if (tcgetattr(fd, &oldtty)