c - Write simultaneousely to two streams -
Is there a way to connect two methods (or file descriptor) together so that writing a stream is also second? (C, linux)
Thank you.
User LaLato is correct, but Linux, the function you are searching for is fopencookie < / Code> is the result of correcting examples of Laalto for Linux:
int my_writefn (Zero * cookie, const char * data, int n) {FILE ** files = ( FILE **) Cookie; Filitt (data, n, 1, file [0]); Return filitt (data, n, 1, file [1]); } Int nop (zero) {return 0; } Cookie_io_functions_t my_fns = {nil *), (zero *) my_writefn, (zero *) noop, (zero *) noop}; File * files [2] = ...; FILE * f = fopencookie ((Zero *) files, "w", my_fns); // ... use the F as you wish ...
When you write to f
, the system will enter your my_writefn
Function To pass things that were passed on fwrite
, to simplify things, you may also want to convert buffering to line file for your file stream:
setvbuf (f, null, _IOLBF, 0);
This is read from any stream connected to the processes (or stdin) until they are sent to a new line output or sent to fwrite
Will buffer the data. Note: You must call sevbuf
after fopencookie
but before any data is written in the stream
I use line buffering because I usually use fopencookie
to redirect to CDSL, or a network socket, and processing line-oriented data. It's easy and more efficient.
Comments
Post a Comment