Article View: comp.lang.c++.moderated
Article #11Re: Re-Routing streams
From: J. Kanze
Date: Mon, 18 Dec 1995 15:41
Date: Mon, 18 Dec 1995 15:41
59 lines
2604 bytes
2604 bytes
X-Original-Date: 18 Dec 1995 11:33:06 +0100
Diego Novillo (diego@cs.ualberta.ca) wrote:
|> X-Original-Date: 15 Dec 1995 05:47:10 GMT
|> lussier@aviator.cis.ufl.edu (Kyle Lussier) writes:
|> >How do I get re-route, or overload the standard streams to send output
|> >to somewhere else? Specifically I want all messages to stderr to be
|> >sent to a file and all messages to stdout to be sent to a file.
|> >
|> Here's one way of doing it. I removed the various consistency checks you
|> may want to do (existence of the file, etc, etc):
|> #include <iostream.h>
|> ofstream cout_file;
|> void redirectOutput(String fname)
|> {
|> cout_file.open(fname);
|> cout = cout_file;
|> }
While this will work for certain implementations (including the
reference implementation which was delivered with CFront), it is
certainly not guaranteed.
In another posting, I posted the solution specified by the standard.
Regretably, of course, this will not work with all implementations yet.
Basically, the only real portable solution in this case is to not use
cout, but a reference or pointer to a ostream, perhaps thus:
ostream&
getOutputStream( char const* filename )
{
if (filename == NULL || strcmp( filename , "-" ) == 0)
return cout ;
else
{
ofstream f( filename ) ;
return f ;
}
}
ostream& output( getOuptutStream( filename ) ) ;
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
-- A la recherche d'une activité dans une region francophone
[ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
[ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
[ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
[ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
Technical Details
Message-ID:
<4b423t$1k@netlab.cs.rpi.edu>Path:
arc1-ca.pugleaf.net!kbad.eglin.af.mil!rpi!not-for-mailReferences:
<4aq1ig$1u2@netlab.cs.rpi.edu> <4arah4$8g8@netlab.cs.rpi.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!kbad.eglin.af.mil!rpi!not-for-mail From: kanze@gabi-soft.fr (J. Kanze) Newsgroups: comp.lang.c++.moderated,comp.lang.c++ Subject: Re: Re-Routing streams Followup-To: comp.lang.c++.moderated,comp.lang.c++ Date: 18 Dec 1995 15:41:49 -0000 Organization: GABI Software, Sarl. Sender: cppmods@netlab.cs.rpi.edu Approved: rmartin@oma.com Message-ID: <4b423t$1k@netlab.cs.rpi.edu> References: <4aq1ig$1u2@netlab.cs.rpi.edu> <4arah4$8g8@netlab.cs.rpi.edu> NNTP-Posting-Host: netlab.cs.rpi.edu