Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirect cout to file

9 views
Skip to first unread message

Alex Vinokur

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
In article <19991012134236...@ng-fz1.aol.com>,
dremt...@aol.com (DREMTIME98) wrote:
[snip]
>
> Now to my question. I wonder how I might be able to direct cout to
not only
> print to the screen but also write to a file. How do you use
redirection for
> that?
>
> Thanks a lot
> Jim.
>
>

Hi,

See below.
Hope this helps.

Alex


//#########################################################
// File#1 of 3 : echo_file.H
//------------------- C++ code : BEGIN -------------------

//###########
// echo_file.H
//###########

#ifndef echo_file_H
#define echo_file_H

#include <string>
#include <iostream.h>
#include <fstream.h>
#include <assert.h>


#define DEFAULT_OutFileName "Default.out"

class echo_file
{

private:
ofstream OutFile;

public:
echo_file ()
{
assert (!OutFile.is_open ());
OutFile.open(DEFAULT_OutFileName);
if (!OutFile.is_open ())
{
cout << "Cannot open "
<< DEFAULT_OutFileName
<< endl;
exit (1);
}
}

void setFileName (char const * const fn)
{
OutFile.close ();
assert (!OutFile.is_open ());
OutFile.open(fn);
if (!OutFile.is_open ())
{
cout << "Cannot open "
<< fn
<< endl;
exit (1);
}
};

~echo_file()
{
OutFile.close();
assert (!OutFile.is_open ());
};

echo_file& operator<< (ostream& v (ostream&))
{
cout << v;
OutFile << v;
return *this;
}

template <class T> echo_file& operator<< (const T& v)
{
cout << v;
OutFile << v;
return *this;
}

};

extern echo_file tout;

#endif

//---------------------------------------------------------
// File#1 of 3 : echo_file.H
//------------------- C++ code : END ----------------------


//#########################################################
// File#2 of 3 : echo_file.C
//------------------- C++ code : BEGIN -------------------

#include "echo_file.H"
echo_file tout;

//---------------------------------------------------------
// File#2 of 3 : echo_file.C
//------------------- C++ code : END ----------------------

//#########################################################
// File#3 of 3 : echo_main.C
//------------------- C++ code : BEGIN -------------------


//###########
// echo_main.C
//###########

#include "echo_file.H"
int main()
{
string s1 = "AAAAA";
char* s2 = "BBBBB";
int i1 = 111;

tout << s1 << endl;
tout << s2 << endl;
tout << "CCCCC" << endl;
tout << i1 << endl;
tout << 222 << endl;

tout.setFileName ("AAA.out");

tout << "HELLO" << endl;
tout << 33333 << endl;

return 0;
}


//---------------------------------------------------------
// File#3 of 3 : echo_main.C
//------------------- C++ code : END ----------------------

//#########################################################
//------------------- Running Results : BEGIN -------------

// Screen
AAAAA
BBBBB
CCCCC
111
222
HELLO
33333


// OutFile : Default.out
AAAAA
BBBBB
CCCCC
111
222


// OutFile : AAA.out
HELLO
33333


//------------------- Running Results : END ---------------

//#########################################################
//------------------- Compiler & System ------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -sr : SunOS 5.6

//---------------------------------------------------------

//#########################################################


Sent via Deja.com http://www.deja.com/
Before you buy.

Dietmar Kuehl

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
Hi,
In article <7u2275$mbp$1...@nnrp1.deja.com>,
Alex Vinokur <alexande...@telrad.co.il> wrote:
> Hope this helps.

Actually, it does not: It points into the false direction, towards lots
of trouble. You should not fiddle with overloading 'operator<< ()' to
write to a special destination. In the context of streams, this operator
is overloaded to format user defined types. To write to a special
destination like eg. simultaneously to two streams a new stream buffer
is created. See my other answer in this thread for details.
--
<mailto:dietma...@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>

Will H. Stewart

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
This is awesome but could you answer my 2 questions? Scan below.
Thanks, Will

Alex Vinokur wrote:
>
> In article <19991012134236...@ng-fz1.aol.com>,
> dremt...@aol.com (DREMTIME98) wrote:
> [snip]
> >
> > Now to my question. I wonder how I might be able to direct cout to
> not only
> > print to the screen but also write to a file. How do you use
> redirection for
> > that?
> >
> > Thanks a lot
> > Jim.
> >

> >******/* Could you please make comments here concerning what this is actually doing. Is it saying when OutFile is not open?*/


>
> Hi,
>
> See below.
> Hope this helps.
>
> Alex
>
> //#########################################################
> // File#1 of 3 : echo_file.H
> //------------------- C++ code : BEGIN -------------------
>
> //###########
> // echo_file.H
> //###########
>
> #ifndef echo_file_H
> #define echo_file_H
>
> #include <string>
> #include <iostream.h>
> #include <fstream.h>
> #include <assert.h>
>
> #define DEFAULT_OutFileName "Default.out"
>
> class echo_file
> {
>
> private:
> ofstream OutFile;
>
> public:
> echo_file ()
> {
> assert (!OutFile.is_open ());

******/* Could you please make comments here concerning what this is
actually doing. Is it saying when OutFile is not open?*/


> OutFile.open(DEFAULT_OutFileName);
> if (!OutFile.is_open ())
> {
> cout << "Cannot open "
> << DEFAULT_OutFileName
> << endl;
> exit (1);
> }
> }
>

> void setFileName (char const * const fn) // Is this * supposed to be a // pointer?????//

Alex Vinokur

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
In article <3804A791...@home.com>,

"Will H. Stewart" <international...@home.com> wrote:

> This is awesome but could you answer my 2 questions? Scan below.
> Thanks, Will
>

[snip]

> > //#########################################################
> > // File#1 of 3 : echo_file.H
> > //------------------- C++ code : BEGIN -------------------
> >
> > //###########
> > // echo_file.H
> > //###########
> >
> > #ifndef echo_file_H
> > #define echo_file_H
> >
> > #include <string>
> > #include <iostream.h>
> > #include <fstream.h>
> > #include <assert.h>
> >
> > #define DEFAULT_OutFileName "Default.out"
> >
> > class echo_file
> > {
> >
> > private:
> > ofstream OutFile;
> >
> > public:
> > echo_file ()
> > {

//===============================================
// For instance, if we add the following line

OutFile.open(DEFAULT_OutFileName);

// (i.e. we create erroneous situation in the program,
// because we open this file below),
//===============================================

> > assert (!OutFile.is_open ()); // Line#31


> ******/* Could you please make comments here concerning what this is
> actually doing. Is it saying when OutFile is not open?*/
>

//===============================================
// we will get the following result of the running
// echo_file.H:31: failed assertion `!OutFile.is_open ()'
// Abort (core dumped)

// If OutFile is not open, the assert command is silent.
//===============================================

> > OutFile.open(DEFAULT_OutFileName);
> > if (!OutFile.is_open ())
> > {
> > cout << "Cannot open "
> > << DEFAULT_OutFileName
> > << endl;
> > exit (1);
> > }
> > }
> >
> > void setFileName (char const * const fn) // Is
this * supposed to be a // pointer?????//

//===============================================
// * is pointer.

// Concerning char const * const // see the thread titled "const char*
const" // ------------------ // in comp.lang.c++ // started 1999/02/08
//
http://www.deja.com/=dnc/[ST_rn=ps]/viewthread.xp?search=thread&recnum=%3c79m
ice$oid$1...@nnrp1.dejanews.com%3e%231/1&AN=441840331&svcclass=dnserver&frpage=g
etdoc.xp // ------------------ // in comp.lang.c++.moderated // started
1999/02/08 //
http://www.deja.com/=dnc/[ST_rn=ps]/viewthread.xp?search=thread&recnum=%3c79m
61o$ema$1...@nnrp1.dejanews.com%3e%231/1&AN=442245779&svcclass=dnserver&frpage=g
etdoc.xp //===============================================


> > {
> > OutFile.close ();
> > assert (!OutFile.is_open ());
> > OutFile.open(fn);
> > if (!OutFile.is_open ())
> > {
> > cout << "Cannot open "
> > << fn
> > << endl;
> > exit (1);
> > }
> > };

[snip]

Alex

0 new messages