Write simultaneousely to two streams

2019-02-23 02:57发布

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.

标签: c linux io stream
5条回答
Ridiculous、
2楼-- · 2019-02-23 03:26

User laalto is correct, but on Linux, the function you are looking for is called fopencookie. Correcting laalto's example for Linux results in:

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]);
}

int noop(void) { return 0; }
cookie_io_functions_t my_fns = {
  (void*) noop,
  (void*) my_writefn,
  (void*) noop,
  (void*) noop
};

FILE *files[2] = ...;

FILE *f = fopencookie((void *)files, "w", my_fns);

// ... use f as you like ...

When you write to f, the system will execute your my_writefn function passing it the data that was passed to fwrite. To make things easier, you may also want to change the buffering for your file stream to be line oriented:

setvbuf(f, NULL, _IOLBF, 0);

That will buffer up the data passed to fwrite until a newline is output or any data is read from any stream attached to the processes (e.g. stdin). NOTE: you must call sevbuf after fopencookie but before any data is written to the stream.

I use line buffering because I usually use fopencookie to redirect stderr to syslog, or over a network socket, and processing line oriented data is easier and more efficient.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-23 03:27

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-02-23 03:34

You can implement something similar to functionality of tee with boost::iostreams.

查看更多
时光不老,我们不散
5楼-- · 2019-02-23 03:44

Not sure if it's what you want, but 'tee' in unix does something similar.

查看更多
对你真心纯属浪费
6楼-- · 2019-02-23 03:48

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.

查看更多
登录 后发表回答