判断STDIN/STDOUT是否被重定向

一直在找这个问题,今天终于在MSDN上面找到了答案,实现代码如下

/* ISATTY.C: This program checks to see whether
 * stdout has been redirected to a file.
 */

#include <stdio.h>
#include <io.h>

void main( void )
{
   if( _isatty( _fileno( stdout ) ) )
      printf( "stdout has not been redirected to a file
" );
   else
      printf( "stdout has been redirected to a file
");
}

此段代码是检测stdout是否已被重定向到文件

_isatty 函数的的功能是如果参数指定的句柄同终端设备相关联,则返回非零值,否则返回零
原文地址:https://www.cnblogs.com/guobbs/p/3580888.html