Sign in to follow this  


Recommended Posts

not sure that this is the correct place for this but i need help in dealing with stdout iknow how to redirect stdout from the console to a file but i need stdout to go to the console AND a log file i cant find any information on google about copying this any help please

Share this post

Link to post
Share on other sites
c++ devc++ windows
yes Oberon thats what i'm trying to do but i dont know how to catch the stuf from cout
all i have been able to find information on is redirecting to a file, nothing more
this is what i found for the file redirection

if( freopen("Logfile.txt", "w", stdout) == NULL)
MessageBox(NULL,"Error Redirecting stdout", "Error", MB_OK);
setbuf( stdout, NULL );

i just cant find out how to intercept stdout
is there a way that stdout will be driected into a function instead of the console?

can i use window's SetStdHandle to set the stdout into a pipe and then read it later?
thats all i cant think of sleep is pulling me down now....

Share this post

Link to post
Share on other sites
May I ask why you're using stdout when you could be using cout just as easily? In fact, its probably a little bit more easier! Just a question though. If you aren't sure on how to use cout, its rather easily:

#include <iostream> // for std::cout, cin, endl
#include <string>
using namespace std;

int main(void)
cout << "Hello World" << endl;
cout << "Hello" << ' ' << "World" << endl;

string szLine("Hello World");

cout << szLine << endl;

Just some examples! Good luck!

Share this post

Link to post
Share on other sites
The easiest method to do this would probably to spawn a child process that used the C++ stream facility to direct output to both a file and stdout, using a pipe to redirect the parent's stdout to the child's stdin.

Here is an article creating a child process with redirected standard in and here is source to a program that would dump stdin to a file and stdout.

#include <iostream>
#include <fstream>

class twinbuf : public std::streambuf {
std::streambuf * buf1_;
std::streambuf * buf2_;
twinbuf(std::streambuf * buf1, std::streambuf * buf2)
: buf1_(buf1), buf2_(buf2) {}

virtual ~twinbuf() {

virtual int_type overflow (int_type c) {
if (c != EOF) {
return c;

virtual int sync () {
if (buf1_->pubsync() == -1) return -1;
if (buf2_->pubsync() == -1) return -1;
return 0;

int main(int argc, char ** argv) {
if (argc != 2) {
return -1;
std::ofstream ofs(argv[1]);
if (!ofs.good()) {
return -1;

twinbuf twin(ofs.rdbuf(), std::cout.rdbuf());
std::cout << std::cin.rdbuf();

return 0;

Share this post

Link to post
Share on other sites
thanks sicrane thats what i was looking for

does the child process continually dump what it recieves or does it need a while/for statement to output the redirected stuff

now i have my main program create a pipe
create the child process and pass the pipe's read handle to the child
and the child dups it to a console and file
until the parent exits or dies

my head hurts

Share this post

Link to post
Share on other sites
With the source code from above, the child will keep dumping as long as std::cin doesn't fail (like with a borken pipe), so you don't need an explicit while loop.

Share this post

Link to post
Share on other sites
ok i have this

int CreateListener(void)
// create a pipe
bool fSuccess;

// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;

// Create a pipe for the child process's STDOUT.
if (! CreatePipe(&piperead, &pipewrite, &saAttr, 0))
return 0;

// set the stdoutput

// start up the child with pipe handle
bool CreateRet = false;

// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

// Set up members of the STARTUPINFO structure.
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
siStartInfo.hStdInput = piperead;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

// Create the child process.
CreateRet = CreateProcess(listenerexe,
listenerlogfile, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION

if (CreateRet == 0)
return 0;
return 1;

the text appears in the console window but the log file is empty and i dont think that the output from my app is traveling through the pipe into the child
in the child process if i add a cout << "whatever" before the std::cout << std::cin.rdbuf();
it appears in the log twice but not in the console
if i place it after std::cout << std::cin.rdbuf(); noting come out any where

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this