Function returning "stream" ?

S

StephQ

At the moment I have a void member function (of a given class) that
takes as input an obj of class type ofstream and write some debug
information to it using the << operator.
I would like to modify the code so that the function returns a
"stream" that can be "sent" to cout or to a file using <<, as this
would give me more flexibility.
I used " " because I'm not sure I'm not using the correct terms
here. :p
Is there an easy way to accomplish this task?
Thank you in advance!

Cheers
StephQ
 
R

Rolf Magnus

StephQ said:
At the moment I have a void member function (of a given class) that
takes as input an obj of class type ofstream and write some debug
information to it using the << operator.
I would like to modify the code so that the function returns a
"stream" that can be "sent" to cout or to a file using <<, as this
would give me more flexibility.
I used " " because I'm not sure I'm not using the correct terms
here. :p

Well, cout and the file actually are the streams.
Is there an easy way to accomplish this task?

What you can do is simply return a string that contains the debug
information. Use a stringstream within your function to fill the string.
 
S

StephQ

What you can do is simply return a string that contains the debug
information. Use a stringstream within your function to fill the string.

The problem (is it?) is that the debug information consists of more
than 1 line of information.
I know that with getline you can at most get an entire line bot not
more.
Does the stringstream library solves the problem?

Thanks
StephQ

p.s. sorry for the initial double post!
 
K

Kai-Uwe Bux

StephQ said:
The problem (is it?) is that the debug information consists of more
than 1 line of information.

And why is that a problem? A std::string may contain any number of
end-of-line characters.
I know that with getline you can at most get an entire line bot not
more. Does the stringstream library solves the problem?

You can write multiple lines into a stringstream. The underlying string will
contain the appropriate end-of-line characters. Writing this string to any
stream will write separate lines.


Best

Kai-Uwe Bux
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top