Connecting Tech Pros Worldwide Help | Site Map

Simple output issue - (I hope)

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:38 PM
Shane
Guest
 
Posts: n/a
Default Simple output issue - (I hope)

Thanks in advance for your help.

I have created several classes that each have their own member
functions that allow them to print the data stored int their
respective classes. In other words, assuming that the object "m" were
a Multimedia object, if I were to call

m.PrintInfo();

I would see output (cout to the console) with the name of the author,
CD title, publishing date, etc.

Now I need to be able to call this xxx.PrintInfo() from another class
and take whatever goes to the console and also log it to a file. Is
there a way to just capture this data and send it to a file?, or do I
have to go write a new print method for each of these classes
(nooooooooo!).

Any point in the right direction would help. By the way, I know how
to read and write to a file, I'm just not sure how to grab the output
to the console and make it also print to a file.

Shane

  #2  
Old July 22nd, 2005, 04:38 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Simple output issue - (I hope)

Shane wrote:[color=blue]
> Thanks in advance for your help.
>
> I have created several classes that each have their own member
> functions that allow them to print the data stored int their
> respective classes. In other words, assuming that the object "m" were
> a Multimedia object, if I were to call
>
> m.PrintInfo();
>
> I would see output (cout to the console) with the name of the author,
> CD title, publishing date, etc.
>
> Now I need to be able to call this xxx.PrintInfo() from another class
> and take whatever goes to the console and also log it to a file. Is
> there a way to just capture this data and send it to a file?, or do I
> have to go write a new print method for each of these classes
> (nooooooooo!).
>
> Any point in the right direction would help. By the way, I know how
> to read and write to a file, I'm just not sure how to grab the output
> to the console and make it also print to a file.[/color]

It would be simpler if your 'PrintInfo' member accepted a single argument,
the stream to output to (and returned the same stream):

ostream& PrintInfo(ostream& os) const {
... // use 'os' to output
return os;
}

In which case you simply pass 'std::cout' to it when outputting to the
"console" and a file stream when outputting to a file.

Another way, of course, is to overload those functions:

void PrintInfo() const {
PrintInfo(std::cout);
}

ostream& PrintInfo(ostream& os) const {
... // use 'os'
return os;
}

Victor
  #3  
Old July 22nd, 2005, 04:38 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Simple output issue - (I hope)

On 19 Jul 2004 11:45:28 -0700, Shane <deja_NOSPAM_@zaft.com> wrote:
[color=blue]
> Thanks in advance for your help.
>
> I have created several classes that each have their own member
> functions that allow them to print the data stored int their
> respective classes. In other words, assuming that the object "m" were
> a Multimedia object, if I were to call
>
> m.PrintInfo();
>
> I would see output (cout to the console) with the name of the author,
> CD title, publishing date, etc.
>
> Now I need to be able to call this xxx.PrintInfo() from another class
> and take whatever goes to the console and also log it to a file. Is
> there a way to just capture this data and send it to a file?, or do I
> have to go write a new print method for each of these classes
> (nooooooooo!).
>
> Any point in the right direction would help. By the way, I know how
> to read and write to a file, I'm just not sure how to grab the output
> to the console and make it also print to a file.
>
> Shane[/color]

There is one way, but I loath to describe it because you should have
written the PrintInfo routines in the correct way in the first place.

void Multimedia::PrintInfo(ostream& out)
{
out << whatever;
}

m.PrintInfo(cout); // print to console
ofstream file("somefile");
m.PrintInfo(file); // print to file

In fact I still think you should write your routines that way. It's not
difficult, especially if you do it from the start.

But if you really want to know here is how to redirect cout to a file.

// make sure console is flushed before we start
cout.flush();

// the file we are going to write to
ofstream file("somefile");

// replace the console buffer with the file buffer, saving the old buffer
streambuf* save_buffer = cout.rdbuf(file.rdbuf());

// now cout will write to the file
cout << "this goes to the file\n";

// restore the old buffer, this is very important!
cout.rdbuf(save_buffer);

This is untested code.

john
  #4  
Old July 22nd, 2005, 04:38 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Simple output issue - (I hope)

* Shane:[color=blue]
>
> I have created several classes that each have their own member
> functions that allow them to print the data stored int their
> respective classes. In other words, assuming that the object "m" were
> a Multimedia object, if I were to call
>
> m.PrintInfo();
>
> I would see output (cout to the console) with the name of the author,
> CD title, publishing date, etc.
>
> Now I need to be able to call this xxx.PrintInfo() from another class
> and take whatever goes to the console and also log it to a file. Is
> there a way to just capture this data and send it to a file?, or do I
> have to go write a new print method for each of these classes
> (nooooooooo!).
>
> Any point in the right direction would help. By the way, I know how
> to read and write to a file, I'm just not sure how to grab the output
> to the console and make it also print to a file.[/color]

Simple redesign: don't have print-functions, use toString() functions
instead -- and in general, _don't_ do i/o in non-i/o classes.

Or, generally not as good, pass a stream argument to those
print-functions.

One reason why toString() is generally better than printOn( aStream ) is
that the former allows you to use the result directly in e.g. a
graphical user interface, whereas with the latter you would have to pass
a std::stringstream or such and then convert to the string you need.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #5  
Old July 22nd, 2005, 04:39 PM
Shane
Guest
 
Posts: n/a
Default Re: Simple output issue - (I hope)

"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<opsbeevjd6212331@andronicus>...[color=blue]
> On 19 Jul 2004 11:45:28 -0700, Shane <deja_NOSPAM_@zaft.com> wrote:
>[color=green]
> > Thanks in advance for your help.
> >
> > I have created several classes that each have their own member
> > functions that allow them to print the data stored int their
> > respective classes. In other words, assuming that the object "m" were
> > a Multimedia object, if I were to call
> >
> > m.PrintInfo();
> >
> > I would see output (cout to the console) with the name of the author,
> > CD title, publishing date, etc.
> >
> > Now I need to be able to call this xxx.PrintInfo() from another class
> > and take whatever goes to the console and also log it to a file. Is
> > there a way to just capture this data and send it to a file?, or do I
> > have to go write a new print method for each of these classes
> > (nooooooooo!).
> >
> > Any point in the right direction would help. By the way, I know how
> > to read and write to a file, I'm just not sure how to grab the output
> > to the console and make it also print to a file.
> >
> > Shane[/color]
>
> There is one way, but I loath to describe it because you should have
> written the PrintInfo routines in the correct way in the first place.
>
> void Multimedia::PrintInfo(ostream& out)
> {
> out << whatever;
> }
>
> m.PrintInfo(cout); // print to console
> ofstream file("somefile");
> m.PrintInfo(file); // print to file
>
> In fact I still think you should write your routines that way. It's not
> difficult, especially if you do it from the sta......[/color]

Yeah, I agree now that you point it out - I'll follow your advice.
This should solve my problem quite nicely, since I just need to
replace the cout with the ostream& variable and I can do whatever I
want with it from the class. I appreciate the help as always!

Shane
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.