确定是否输出过程在C / C被重定向++ [英] Determine whether process output is being redirected in C/C++

查看:109
本文介绍了确定是否输出过程在C / C被重定向++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写Linux的命令行工具。
如果输出(stdout)是要一个shell这将是很好打印一些逃脱上色输出。但是,如果输出被重定向这些庆典逃逸不应该是打印或内容可能会破坏依赖于该输出分析器。

I'm writing command line utility for Linux. If the output (stdout) is going to a shell it would be nice to print some escapes to colorize output. But if the output is being redirected those bash escapes shouldn't be print, or the content might break parsers that rely on that output.

有几个程序,做到这一点(如吮吸 ACK ),但我发现的那些是用Perl编写的,我无法了解他们是如何做到的。

There are several programs that do this (suck as ack) but the ones I found were written in Perl and I couldn't find out how they did it.

我想用C / C ++写我的工具。

I wanted to use C/C++ to write my utility.

推荐答案

您可以使用 isatty 在Linux上。此功能显然是不标准的C,因为 - 例如 - 在许多平台上,你不能将输出重定向到一个文件

You can use isatty on linux. This function is obviously not standard C, since - for example - on many platforms you can't redirect the output to a file.

这篇关于确定是否输出过程在C / C被重定向++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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