Topic: iostreams


Author: huber@kazoo.cs.uiuc.edu (Jay Huber)
Date: 2 Mar 1995 21:28:53 GMT
Raw View
You can derive stream buffers to perform special
functions.  Here is a working example of how to
get a null buffer (output goes nowhere), and a
tee buffer (output goes to each of two other streams).

This ought to work with most iostrams implementations,
but I can't make any promises...


// BEGIN

#include <iostream>

class nul_buf : public streambuf {
public:
  virtual int overflow(int c = EOF) { return c; }
  };


class tee_buf : public streambuf {
  ostream & t1;
  ostream & t2;

public:
  tee_buf(ostream & _t1, ostream & _t2) :
    t1(_t1), t2(_t2) {}

  virtual int overflow(int c = EOF) {
    t1.put(c); t2.put(c);
    return c;
    }

  virtual int sync() { t1.flush(); t2.flush(); return 0; }
  };

// END


Jay Huber
huber@cs.uiuc.edu




Author: fraley@hpl.hp.com (Bob Fraley)
Date: Thu, 2 Mar 1995 19:37:15 GMT
Raw View
Does anyone know of a way to direct stream output to 2 files
simultaneously?  I'm looking for functionality similar to that of the
Unix "tee" command.

Specifically, I'd like to have cout simultaneously display on the
screen, and capture the output to a file for future printing.

If possible, it would be nice to have the cin file echo to the
printing file as well.

I know that it is possible to change the buffer associated with cout
to write to a separate file, but then interactive operation isn't
possible.

Thanks for any suggestions.

Bob Fraley
HP