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?
“>>” 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.
“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.
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.
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.
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.) */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With