#### Archived

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

# Capturing Standard Output

## Recommended Posts

I want to write anything that my programm COUTs and PRINTFs and maybe CERRs in my graphical console, is it possible to do this? The Wymsical Wyvern

##### Share on other sites
program.exe 1>stdout.txt 2>stderr.txt

It redirects standard output to the file stdout.txt, and stderr to the file stderr.txt.

##### Share on other sites
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 on other sites
kk, i think i understand, but rdbuf() gets not sets maybe another function? or maybe i don''t understand?

the wymsical wyvern

##### 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 on other sites
Its not documented and when i tried giving it a parameter it says: rdbuf() does not take 1 parameter

##### 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 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 on other sites
i''m not using i''m using so i guesse thats my problem

• ### Forum Statistics

• Total Topics
628401
• Total Posts
2982463

• 9
• 10
• 9
• 19
• 24