It is rather easy to redirect output to another destination. I can easily achieve that with:
freopen ("/def/tty0", "w", stdout);
printf ("Redirected");
fclose (stdout);
But how could I redirect it to two or more files? I need functionality similar to this provided by "tee".
Is there any well known approach?
This is not possible to do directly: Each file descriptor is assigned to exactly one resource (typically a file). If you want a write to a FILE
yield in writes to multiple files, you have to do some tricks. Here are some ideas:
fopencookie(3)
to make a FILE*
that actually outputs to more than one file. See the manpage of fopencookie(3)
for more details.tee(1)
with suitable arguments and write the data to the standard input of tee(1)
. Have a look at popen(3)
for a useful function to do that.