|
蓝森林 http://www.lslnet.com 2006年6月6日 10:18
c程序与标准输入
请问如何在c程序里面实现对用户每次按键输入都马上接收以进行处理,
而不是在回车以后?
谢谢!
|
c程序与标准输入
getc(stdin) |
c程序与标准输入
mygod错了!getc()虽然不要回车,但要让系统处理必须调皮个回车,不信你可以去试一下。
至于该怎样才能做到这一点,我也不知道。 |
c程序与标准输入
那就是getch()了,反正是两者之一!
小问题 |
c程序与标准输入
用IOCTL还是FCNTCL的(记不清了)设置输入端口参数 |
c程序与标准输入
程序实现:
#include <stdio.h>;
#include <termio.h>;
static struct termio ttysave ;
void restore() ;
char getch() ;
main()
{
puts( "---------------------------" ) ;
puts( "enter a key : " );
getch() ;
puts( "\n--------------" );
exit( 0 );
}
char getch()
{
static char ch ;
static int total, flag = 1 ;
struct termio tty ;
if( flag )
{
flag = 0 ;
if( ioctl( 0, TCGETA, &tty ) == -1 )
{
perror( "ioctl" );
exit( -1 );
}
ttysave = tty ;
tty.c_lflag &= ~( ICANON | ECHO | ISIG ) ;
tty.c_cc[VMIN] = 1 ; // MIN
tty.c_cc{VTIME] = 0 ; // TIME
if( ioctl( 0, TCSETAF, &tty ) == -1 )
{
restore();
perror( "ioctl" );
exit( -2 );
}
}
switch( total = read( 0, &ch, 1 ))
{
case -1 :
restore() ;
perror( "read" );
exit( 3 );
case 0:
restore():
fputs( "EOF error!", stderr );
exit( 4 ) ;
default:
.....
}
restore();
return( ch );
}
void restore()
{
if( ioctl( 0, TCSETAF, &ttysave ) == -1 )
{
perror( "ioctl" );
exit( 5 );
}
return ;
}
|
c程序与标准输入
其说明可参见《深入学习UNIX》清华大学出版社 刘祖亮编著 1997年8月第一版 |
c程序与标准输入
谢谢各位!!
getch()的确可以,相信vt8000提供的代码也没有问题,因为我是看到类似的程序才提这个问题的~~
那么能否再讲讲这两者之间的异同呢?
|
c程序与标准输入
在UNIX里并没有直接的getch,能读入一个字符而不回车自行进行读取工作。
另外一种方法是利用curses库,再用getch():
#include <curses.h>;
#include <sys/select.h>;
int Zinkey()
{
int k;
fd_set f;
struct timeval t;
FD_ZERO( &f );
FD_SET( 0, &f );
t.tv_sec = 0;
t.tv_usec = 100000L;
if( !select( 1, &f, 0, 0, &t ) )
return( 0 );
if( ( k = getch() ) == ERR )
k = 0;
return( k );
}
|
c程序与标准输入
getchar()可以读入int吗? |
c程序与标准输入
不可以 |
c程序与标准输入
被蛤蟆吃到的天鹅不是好天鹅。 |
c程序与标准输入
getchar()如若我强行打入一窜int,
是不是会转换成char? |
c程序与标准输入
??? |
c程序与标准输入
我想问一下,linux下的getch()怎样使用,上面的程序抱错,说if( ( k = getch() ) == ERR )中有问题?具体为undefine refer to stdscr
undefine refer to wgetch,
具体怎么解决。快帮忙?本人非常感谢!!!
|
c程序与标准输入
-->
add param -lcurses when compile. |
c程序与标准输入
while( 1)
{
if(kbhit())
*c++ = getch();
if( *c == '\n')
dosomething();
}
我糊编的没准可以完美工作 ^.^ |
c程序与标准输入
按VT8000提供的方法(include <curses.h>;)使用函数编译不能通过!
怎么回事? |
c程序与标准输入
按VT8000提供的方法(include <curses.h>;)使用函数编译不能通过!
怎么回事? |
c程序与标准输入
按VT8000提供的方法(include <curses.h>;)使用函数编译不能通过!
怎么回事? |
| |