开发者

Is stdin and console and keyboard input same

Is stdin and console and key开发者_Go百科board input same in C?


From the isatty(3) man page:

The isatty() function tests whether fd is an open file descriptor referring to a terminal.

   #include <unistd.h>

   int isatty(int fd);

isatty() returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error.

So, isatty(stdin) will return nonzero if it is being typed, and zero if it's being redirected.


Yes, that's correct. You can essentially have a read() system call to accept input from stdin.


Not necessarily. stdin can be redirected from a file (e.g. executable < input) or some other non-interactive device.

amphetamachine's answer gives you the tool to tell the difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜