Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

output

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?

like image 880
microo8 Avatar asked Feb 07 '13 06:02

microo8


People also ask

How do I redirect an output to a file?

“>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents. “>” operator is used to redirect the command's output to a single file and replace the file's current content.

What command redirect output to a file and displays the same contents on the screen?

“tee” command is one of the most valuable tools that helps a Linux user redirect the output of a command to a file and screen.

What character can you use the redirect the output of a program to a file?

The '>' symbol is used for output (STDOUT) redirection. Here the output of command ls -al is re-directed to file “listings” instead of your screen. Note: Use the correct file name while redirecting command output to a file.

How do you redirect the output of a command to a file in Linux?

Option One: Redirect Output to a File Only To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.


1 Answers

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.) */
}
like image 87
Some programmer dude Avatar answered Oct 13 '22 04:10

Some programmer dude