C ++检测输入重定向 [英] C++ Detect input redirection

查看:178
本文介绍了C ++检测输入重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
Detect if stdin is a terminal or pipe in C/C++/Qt?

$ b考虑我们有一个小程序,它需要一些标准的C输入。

Consider we got a small program which takes some standard C input.

我想知道用户是否正在使用输入重定向,例如像这样:

I would like to know if the user is using input redirection, for example like this:

./programm < in.txt

是否有办法检测 程序中?

Is there a way to detect this way of input redirecting in the program?

推荐答案

没有可移植的方法, cin 来自。在Posix系统上,您可以测试 cin 是否来自终端,或者使用 isatty ,类似这样:

There's no portable way to do that, since C++ says nothing about where cin comes from. On a Posix system, you can test whether or not cin comes from a terminal or is redirected using isatty, something like this:

#include <unistd.h>

if (isatty(STDIN_FILENO)) {
    // not redirected
} else {
    // redirected
}

这篇关于C ++检测输入重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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