I've been looking and I'm a bit lost about how to change the target of ostream so that I could write to other buffers than the console.

7 Years
Discussion Span
Last Post by mike_2000_17

The streams work on a streambuf object. The streambuf is an abstract base class that provides a buffer interface for use by the stream objects (like ostream and istream). Two derived class are provided by the standard, the filebuf and stringbuf classes (where, obviously, the filebuf reads/writes to a file and the stringbuf reads/writes to a string (or character array)).

To associate a stream with a given streambuf object, you can use the rdbuf() function.

If you want to actually create your own streambuf derived class, then I don't really know how to do that, it is probably pretty hard. But usually, either the filebuf or stringbuf should be sufficient for a given application, for instance, most OSes implement things like COM-ports, internet sockets, or console/terminal IO as files (things like look like files anyways, pseudo-files if you want).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.