代码: 1.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
if(!isatty(fileno(stdout)))
{
fprintf(stderr,"You are not a terminal\n");
exit(1);
}
exit(0);
}
gcc 1.c -o t
./t
./t > file 输出You are not a terminal
分析:isatty检测标准输出流stdout是否连接到终端,isatty的参数是文件描述符,而stdout是文件流,
所以通过fileno(stdout)获得文件描述符。
若标准输出流stdout没有连接到终端,则说明标准输出被重定向了。
本文介绍了一个简单的C程序,用于检查标准输出是否被重定向。通过使用isatty和fileno函数,程序能够判断其输出是否直接连接到终端设备。如果标准输出被重定向,则输出提示信息。
994





