Having found out that the output of my program is redirected to a file

I want to know if it is possible in linux and C to find out that the output of my programs is redirected to a file. I want to format the output that is read by man when it prints to stdout $ ./myprogram, and like csv when it is redirected to a file$ ./myprogram >> data.csv

is it possible?

+5
source share
1 answer

You can use the function isattyfor this:

if (isatty(STDOUT_FILENO))
{
    /* Standard out is an interactive terminal */
}
else
{
    /* Standard out is something else (pipe, file redirect, etc.) */
}
+10
source

All Articles