检测stdin是终端还是管道? [英] Detect if stdin is a terminal or pipe?
本文介绍了检测stdin是终端还是管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从没有参数的终端执行 python
时,会显示Python交互式shell。
When I execute "python
" from the terminal with no arguments it brings up the Python interactive shell.
当我从终端执行 cat | python
时,它不会启动交互模式。
When I execute "cat | python
" from the terminal it doesn't launch the interactive mode. Somehow, without getting any input, it has detected that it is connected to a pipe.
如何在C或C ++或Qt中进行类似的检测?
How would I do a similar detection in C or C++ or Qt?
推荐答案
使用 isatty
:
#include <stdio.h>
#include <io.h>
...
if (isatty(fileno(stdin)))
printf( "stdin is a terminal\n" );
else
printf( "stdin is a file or a pipe\n");
(在窗口中,它们以下划线为前缀: _isatty
, _fileno
)
(On windows they're prefixed with underscores: _isatty
, _fileno
)
这篇关于检测stdin是终端还是管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文