Thread: Output redirection

  1. #1
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26

    Output redirection

    Is there a way for a C program to determine whether its output to stdout is being redirected?

    (I would like to be able to display to stderr more warning messages if it is redirected than if not).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    The standard unix-y way is
    NAME
    isatty - test whether a file descriptor refers to a terminal

    SYNOPSIS
    #include <unistd.h>

    int isatty(int fd);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    738
    The more unixy way would be to take a flag (say, -v or --verbose) to increase the level or quantity of messages written to stderr. Otherwise a user may be somewhat surprised to find that more is written to a file or pipe than to the terminal. (Part of the Unix philosophy is "principle of least astonishment", which means that consistency is valued.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input and output redirection together !!
    By prashant8809 in forum C Programming
    Replies: 2
    Last Post: 09-08-2012, 08:56 AM
  2. input output redirection
    By chess2009 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 06:46 PM
  3. Dos Redirection
    By En-Motion in forum C Programming
    Replies: 3
    Last Post: 10-07-2008, 11:24 PM
  4. Help with redirection
    By Nornny in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2004, 07:25 AM

Tags for this Thread