microo8
microo8

Reputation: 3774

Finding out that the output of my program is redirected into a file

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?

Upvotes: 5

Views: 130

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409206

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.) */
}

Upvotes: 10

Related Questions