Capturing Standard Output

Started by
7 comments, last by Wymsical 22 years, 5 months ago
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
Advertisement
program.exe 1>stdout.txt 2>stderr.txt

It redirects standard output to the file stdout.txt, and stderr to the file stderr.txt.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
kk, i think i understand, but rdbuf() gets not sets maybe another function? or maybe i don''t understand?

the wymsical wyvern
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Its not documented and when i tried giving it a parameter it says: rdbuf() does not take 1 parameter
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
Peter Windridgehttp://www.incomplete.co.uk/ .. just started DirectX and general game programming so please excuse stupid questions.
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
i''m not using i''m using so i guesse thats my problem

This topic is closed to new replies.

Advertisement