[comp.lang.c++] Assigning cout in Turbo C++

walkerd@maccs.dcss.mcmaster.ca (David Walker) (03/14/91)

Summary : Can you reassign cout to a file?
Expires: 
Sender: walkerd@maccs.mcmaster.ca
Followup-To: 
Distribution: comp.lang.c++
Organization: McMaster University, Hamilton, Ontario, Canada
Keywords: 

I have not been reading this newsgroup lately, so please forgive me if this
has already come up :

I am trying to reassign cout to a file in Turbo C++ 2.0, but I can't even 
determine if it is possible. I know that this is possible using stdout; it is   the example of freopen on page 172 of the Reference Guide. I would like to do
the same thing with cout, and since I have a LOT of code using cout, it would
be a real pain to go back and use stdout. Borland's tech support think that 
this can be done, but can't tell me how to do it. All I want to do is reassign
cout to a file so that all screen output becomes file output. If anyone can tellme how to do this, I will be very grateful!

Dave Walker

ahodgson@athena.mit.edu (Antony Hodgson) (03/16/91)

In article <27DEC60D.18984@maccs.dcss.mcmaster.ca> walkerd@maccs.dcss.mcmaster.ca (David Walker) writes:
>Summary : Can you reassign cout to a file?

>All I want to do is reassign cout to a file so that all screen
>output becomes file output.

I'm not sure if you can simply assign cout to a file, but the following
works (I think - I did it once and don't have my code close at hand) and
probably does what you want.

	ofstream f( "output.fil" );
	ostream sout;  // for Standard OUT

	//  put to the screen
	sout = cout;
	sout << "Hi there";

	//  to a file
	sout = f;
	sout << "Into the file";

Hope this helps.

Tony Hodgson
ahodgson@hstbme.mit.edu