繁体   English   中英

在C中读取stdin文件描述符0中的值

[英]read the value in stdin file descriptor 0 in C

我知道在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 read

小心! 不要使用scanf因为它的缓冲会干扰select

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM