I'm working on a piece of code that is intended to capture standard cout and cerr streams and redirect the output to anywhere I want. So what I want to do is to be able to write something like:
cout << "anything" << endl;
Then I want to be able to get the string which would normally be output to - wherever it normally goes. Like in a console application the standard output would normally be printed to the console window. I need access to the string so I can put it elsewhere.
So I've started out with my references on rdbuf which I am assured allow me to install my own streambuf object. No problem, I've written the beginning of a class which inherits from stringbuf. Now I'm just a little bit stuck because I dont know what to override to intercept the usual code flow and achieve my goal of obtaining that string.
So far I've tried overriding sputc and sputn but that hasn't worked. This is one of those esoteric C++ features that it seems to be quite hard to find much in the way of references which is why I come here. In case it makes a difference I'm working with C++ in VS2005.
All comments appreciated, but the points will go to anyone who can provide me with the information I need to write a working solution. Thanks for reading!
Start Free Trial