🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #12

Re: Re-Routing streams

#12
From: J. Kanze
Date: Mon, 18 Dec 1995 13:36
51 lines
2715 bytes
X-Original-Date: 18 Dec 1995 11:38:09 +0100

Dietmar Kuehl (kuehl@uzwil.informatik.uni-konstanz.de) wrote:
|> X-Original-Date: 15 Dec 95 16:50:42 GMT

|> Hi,
|> Diego Novillo (diego@cs.ualberta.ca) wrote:
|> : X-Original-Date: 15 Dec 1995 05:47:10 GMT

|> : #include <iostream.h>
|> : ofstream     cout_file;

|> : void  redirectOutput(String fname)
|> : {

|> :    cout_file.open(fname);
|> :    cout = cout_file;
|> : }

|> This is not portable as the assignment is not part of the Standard C++
|> Library although some current implementations allow such an assignment
|> (I'm not sure whether a conforming compiler/library is allowed to
|> accept the above code). You can replace the used 'streambuf' using the
|> 'rdbuf()' function. Thus you can replace the line with the assignment
|> by the following one:

|>      cout.rdbuf(cout_file.rdbuf());

|> This is portable. Note that you might want to remember the original
|> 'streambuf' used and restore it prior to program termination.

A more accurate description would be: this *should* be portable.  As far
as I can tell, there are not very many compilers at present which
actually implement this.

At present, there is no portable solution to the problem as stated.
There are implementations under which neither of the above solutions
work.  The best solution is probably to ask yourself: why do I want to
do this?  In most cases, it is trivial to rewrite the code to use a
reference to an ostream everywhere, instead of cout.  Then just
conditionally seat the reference to whichever stream you want.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
              -- A la recherche d'une activiti 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: <4b3qoe$s6s@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
References: <4aq1ig$1u2@netlab.cs.rpi.edu> <4arah4$8g8@netlab.cs.rpi.edu> <4aup7l$ioa@netlab.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.lang.c++.moderated,comp.lang.c++
Subject: Re: Re-Routing streams
Date: 18 Dec 1995 13:36:14 -0000
Organization: GABI Software, Sarl.
Sender: cppmods@netlab.cs.rpi.edu
Approved: Dietmar.Kuehl@uni-konstanz.de
Message-ID: <4b3qoe$s6s@netlab.cs.rpi.edu>
References: <4aq1ig$1u2@netlab.cs.rpi.edu> <4arah4$8g8@netlab.cs.rpi.edu> <4aup7l$ioa@netlab.cs.rpi.edu>
NNTP-Posting-Host: netlab.cs.rpi.edu