使用select函数循环读取键盘输入
来源:互联网 发布:人人视频 mac客户端 编辑:程序博客网 时间:2024/05/16 15:17
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
int main()
{
int keyboard;
int ret,i;
char c;
fd_set readfd;
struct timeval timeout;
//非阻塞地读取终端上的输入信息,/dev/tty当前终端
keyboard=open("/dev/tty",O_RDONLY | O_NONBLOCK);
//如果条件返回错误,则终止程序执行
assert(keyboard>0);
while(1)
{
timeout.tv_sec=5;//超时时间设置为5s
timeout.tv_usec=0;
FD_ZERO(&readfd); //清空可读的fd集合
//把打开终端的fd加入到可读fd集合中
FD_SET(keyboard,&readfd);
//调用select查看是否有可读的fd
ret=select(keyboard+1, &readfd, NULL, NULL, &timeout);
//表示调用select失败
if(ret==-1)
perror("select error");
else if(ret)
{
//判断终端的fd是否在可读描述符集合中
if(FD_ISSET(keyboard,&readfd))
{
i=read(keyboard, &c, 1);
if(c=='\n')
continue;
printf("The input is %c\n",c);
if(c=='q')
break;
}
}
//表示等待时间超时
else if(ret==0)
printf("time out\n");
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
int main()
{
int keyboard;
int ret,i;
char c;
fd_set readfd;
struct timeval timeout;
//非阻塞地读取终端上的输入信息,/dev/tty当前终端
keyboard=open("/dev/tty",O_RDONLY | O_NONBLOCK);
//如果条件返回错误,则终止程序执行
assert(keyboard>0);
while(1)
{
timeout.tv_sec=5;//超时时间设置为5s
timeout.tv_usec=0;
FD_ZERO(&readfd); //清空可读的fd集合
//把打开终端的fd加入到可读fd集合中
FD_SET(keyboard,&readfd);
//调用select查看是否有可读的fd
ret=select(keyboard+1, &readfd, NULL, NULL, &timeout);
//表示调用select失败
if(ret==-1)
perror("select error");
else if(ret)
{
//判断终端的fd是否在可读描述符集合中
if(FD_ISSET(keyboard,&readfd))
{
i=read(keyboard, &c, 1);
if(c=='\n')
continue;
printf("The input is %c\n",c);
if(c=='q')
break;
}
}
//表示等待时间超时
else if(ret==0)
printf("time out\n");
}
return 0;
}
0 0
- 使用select函数循环读取键盘输入
- Python 读取键盘输入 :raw_input()和input() 函数 读取交互输入
- C/C++有关对键盘输入值的读取函数
- 在循环中使用Select 函数,进行等待超时的使用注意点
- 键盘输入循环缓冲区问题
- 循环从键盘输入
- 如何使用select()函数
- select 函数 使用
- select函数的使用
- select函数的使用
- select函数使用总结
- select()函数使用
- select函数使用总结
- select函数使用注意
- select函数的使用
- select函数使用浅析
- select函数的使用
- select函数使用
- JAVA 异常
- 循序渐进学用MAT排查Android Activity内存泄露
- Java中wait()和sleep()的区别
- 自定义Button
- redis cluster 集群 安装 配置 详解
- 使用select函数循环读取键盘输入
- JAVA 集合
- 解决docker中/etc/default/docker配置DOCKER_OPTS 失效问题
- 硬币趣味题
- 第二周项目1--C/C++语言中函数参数传递的三种方式
- Leetcode3:Longest Substring Without Repeating Characters
- 模拟消息队列
- ElasticSearch知识点整理
- Sharping Up with Angularjs 1.x -Direactive