Archived

This topic is now archived and is closed to further replies.

Wymsical

Capturing Standard Output

Recommended Posts

While Martee''s suggestion will work, I think a more generic solution would be to derive a class from basic_streambuf and attach it to the stream objects with the rdbuf method, eg
  
MyStreamBuf* strbuf = new MyStreamBuf();
cout.rdbuf( strbuf );
cin.rdbuf( strbuf );

For more information on the interaction between the stream objects and the streambuf objects, you might want to check out this article. It goes into some detail on how to extend and customize the iostreams library.



"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites
rdbuf() is an overloaded method. Without parameters it just returns the current streambuf, while with a parameter you can use it to set a new one(I believe it still returns the old one, tho)

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites
you can create pipes for stderror and stdout, set STARTUPINFO with dwFlags STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW, hStdOutput to your output 'handle', hStdError for errors and I have wShowWindow = SW_HIDE because sometimes the console momentarily pops up.
You must close the handles, decremementing the use count so that the console program can exit because it hasn't got handles open on it.
Then create the process and use 'ReadFile' to get the output.


This would obviously mean writign a seperate exe for it which after I read the thread again probably isn't what you want.
For printf, you could just #define it to something else.

Edited by - Peterw on November 10, 2001 5:44:47 PM

Share this post


Link to post
Share on other sites
I checked, and for my compiler(MSVC++.NET) rdbuf() is overloaded for 0 or 1 parameters, the 1 parameter being a basic_streambuf. What compiler are you using? Also make sure you are using the newer version of the iostreams library, eg <iostream> instead of <iostream.h>
I hope you do realize that you have to create a custom subclass of basic_streambuf to get this to work. This isnt exactly trivial, but if you read that article I gave you a link to, it should get you most of the way there.

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites