如果检测标准输入是C / C ++ / Qt的一个终端或管道? [英] Detect if stdin is a terminal or pipe in C/C++/Qt?

查看:152
本文介绍了如果检测标准输入是C / C ++ / Qt的一个终端或管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行蟒蛇由不带参数的终端它带来了Python的交互式shell。

When I execute "python" from the terminal with no arguments it brings up the Python interactive shell.

当我执行猫|蟒蛇从终端没有启动交互模式。不知何故,没有得到任何输入时,它已检测到它被连接到管道

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");

(在Windows上他们是prefixed用下划线: _isatty _fileno

这篇关于如果检测标准输入是C / C ++ / Qt的一个终端或管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆