This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Redirecting I/O
- From: Cary Coutant <ccoutant at google dot com>
- To: Arthur Schwarz <aschwarz1309 at verizon dot net>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 10 Apr 2009 11:36:01 -0700
- Subject: Re: Redirecting I/O
- References: <969649.86751.qm@web84008.mail.mud.yahoo.com>
(This question probably should be on gcc-help instead of this list.)
> I'm trying to redirect I/O in my C++ application and am having some
> difficulty. I am trying to use cout or a file for output based on some
> condition. cout is an ostream object and file is an ofstream object.
> The types are incompatible, as in:
>
> bool condition;
> ofstream x;
> ofstream out = (condition)?cout: x; ? // won't work because of cout
Try:
ostream& out = condition ? cout : x;
> In addition I would like to redirect an ofstream object to be the same as
> out, as in;
>
> void fn() { object = out; } // won't work because '=' is private.
Again, try declaring object as "ostream&".
-cary