[英]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.