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

时间:2013-09-26 19:11:43

标签: c pipe stdin file-descriptor

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

1 个答案:

答案 0 :(得分:0)

使用文件描述符read中的0

小心!请勿使用scanf,因为其缓冲可能会干扰select