使用select函数循环读取键盘输入

 1 #include <sys/time.h>
 2 #include <stdio.h>
 3 #include <sys/types.h>
 4 #include <sys/stat.h>
 5 #include <fcntl.h>
 6 #include <assert.h>
 7 #include <stdlib.h>
 8 #include <string.h>
 9 #include <unistd.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <sys/select.h>
15 
16 int main()
17 {
18     int keyboard;
19     int ret,i;
20     char c;
21     fd_set readfd;
22     struct timeval timeout;
23     keyboard = open("/dev/tty",O_RDONLY|O_NONBLOCK);
24     assert(keyboard>0);
25     while(1){
26         timeout.tv_sec = 1;
27         timeout.tv_usec = 0;
28         FD_ZERO(&readfd);
29         FD_SET(keyboard,&readfd);
30         ret = select(keyboard+1,&readfd,NULL,NULL,&timeout);
31         if(FD_ISSET(keyboard,&readfd)){
32             i = read(keyboard,&c,1);
33             if('\n'==c)
34                 continue;
35             printf("The input is %c\n",c);
36             if('q'==c)
37                 break;
38         }
39     }
40     return 0;
41 }

 

转载于:https://www.cnblogs.com/--lr/p/11295798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值