当我在没有参数的情况下从终端执行“python
”时,它会打开 Python 交互式 shell。
当我从终端执行“cat | python
”时,它不会启动交互模式。不知何故,在没有得到任何输入的情况下,它检测到它已连接到管道。
如何在 C 或 C++ 或 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");
(在 Windows 上,它们以下划线为前缀:_isatty
、_fileno
)
关于c++ - 检测标准输入是终端还是管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1312922/