Simple output issue - (I hope)

S

Shane

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
 
V

Victor Bazarov

Shane said:
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.

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
 
J

John Harrison

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

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
 
A

Alf P. Steinbach

* Shane:
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.

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.
 
S

Shane

John Harrison said:
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......

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top