[ale] Q: directing stream I/O _the_C++_way_

John Mills johnmills at speakeasy.net
Sat Nov 27 21:16:43 EST 2004


Fletch -

Thanks for the answer.

On Sat, 27 Nov 2004, Fletch wrote:

> >>>>> "John" == John Mills <johnmills at speakeasy.net> writes:
> 
>     John> ALErs - I want to optionally start a C++ program with the
>     John> name of a file for its log activity, else have the output
>     John> sent to the console as presently.  (I use 'cout'). What's a
>     John> simple way to alternatively connect an output stream to
>     John> 'stdout' or a named file? (For my own learning, I would
>     John> rather use "C++-style" than "C-style" operators.)

> I want to say that you should be able to declare an ostream and assign
> either an ofstream or cout as desired.
 
> ostream& log;
> 
> if( logfilename ) {
>   ofstream logfile( logfilename.c_str() );
>   log = logfile;
> } else {
>   log = cout;
> }

That's what I was hoping for - I was trying to guess the syntax, but 
hadn't that I could use 'cout' like that.

> Granted that's not perl so I can't guarantee the syntax is exactly
> correct . . . :)

Sorry - I'm not smart enough to ask Perl questions.
 
>     John> A pointer to a nice on-line manual for this type inquiry
>     John> would also be nice.
 
> Not online, but several people at work have this book and it's been
> helpful the handfull of times I've needed to poke at something C++.
 
> _The C++ Standard Library: A Tutorial and Reference_
> http://www.amazon.com/exec/obidos/tg/detail/-/0201379260

Thanks for this also. I'm still with Stroustrup's and Lippman's books - 
good, but didn't get me over this.

 - Mills
   john.m.mills at alum.mit.edu



More information about the Ale mailing list