Is there a way to to couple two streams (or file descriptors) together so that writing to one stream will also write to the second one? (C, Linux)
Thanks.
Is there a way to to couple two streams (or file descriptors) together so that writing to one stream will also write to the second one? (C, Linux)
Thanks.
You can implement something similar to functionality of tee
with boost::iostreams.
Not sure if it's what you want, but 'tee' in unix does something similar.
The first thing that came to mind to me was also "tee". So, let's combine C and the shell with popen:
FILE * multi_out;
multi_out = popen( "tee file1.out > file2.out", "w");
/* error checks, actual work here */
pclose( multi_out);
/* error checks here */
As a Unix bigot, I have assumed you are not trying this on Windows.
Use funopen
or fwopen
and supply your own write function that writes to multiple FILE*
s.
Example:
FILE *files[2] = ...;
FILE *f = fwopen((void *)files, my_writefn);
// ... use f as you like ...
int my_writefn(void *cookie, const char *data, int n) {
FILE **files = (FILE **)cookie;
fwrite(data, n, 1, files[0]);
return fwrite(data, n, 1, files[1]);
}
(Error handling omitted.)
Note that funopen
and fwopen
are BSD and not in standard Linux. I'm not aware if there's a Linux-compatible equivalent.