我知道在C中stdin文件描述符是0.我将文件描述符0附加到列表中并使用select()来检测控制台是否有任何输入。
它可以成功检测到有输入。但问题是如何检索价值以及如何检索? (代码忽略错误检查)
fd_set master;
int console=0; //my stdin file descriptor
fdmax = console + 1;
FD_SET(console,&master);
select(fdmax+1, &read_fds, NULL, NULL, NULL);
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &master)){
printf("input detected!\n");
dump_line(stdin); //clear the buffer
//do w.e here
}
答案 0 :(得分:0)
使用文件描述符read
中的0
。
小心!请勿使用scanf
,因为其缓冲可能会干扰select
。