5

I want to know if it is posible in linux and C to find out that my programs output is redirected to a file. I want to format the output human readable when it is printed on stdout $ ./myprogram and like csv when it is redirected to a file $ ./myprogram >> data.csv

is it posible?

microo8
  • 3,568
  • 5
  • 37
  • 67

1 Answers1

10

You can use the isatty function for that:

if (isatty(STDOUT_FILENO))
{
    /* Standard out is an interactive terminal */
}
else
{
    /* Standard out is something else (pipe, file redirect, etc.) */
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621