473,508 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function returning "stream" ?

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
Stefano

Mar 6 '07 #1
5 5870
StephQ wrote:
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
What about returning a std::string? You could send that to cout or a file
using <<. So it seems to fit the description.

Alternatively, you could also make your function use an ostream & instead of
a ofstream &.
Best

Kai-Uwe Bux
Mar 6 '07 #2
On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
StephQ wrote:
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

What about returning a std::string? You could send that to cout or a file
using <<. So it seems to fit the description.

Alternatively, you could also make your function use an ostream & instead of
a ofstream &.

Best

Kai-Uwe Bux
See my reply above.
I have never used ostream before. Do you have a link to a reference /
tutorial about it and streams in general?

Best
StephQ

Mar 6 '07 #3
StephQ wrote:
On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>StephQ wrote:
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

What about returning a std::string? You could send that to cout or a file
using <<. So it seems to fit the description.

Alternatively, you could also make your function use an ostream & instead
of a ofstream &.

Best

Kai-Uwe Bux

See my reply above.
See my reply to that reply.

I have never used ostream before. Do you have a link to a reference /
tutorial about it and streams in general?
Unfortunately, computer books are incredibly booring to me (with very,
_very_ few exceptions). I don't read them unless I have to look up
something. For that, I usually refer to the C++ standard. However, it
appears to be general consensus in this forum that this is not a good
recommendation.

However, for the case you mentioned, I take it that you have some function

void log ( std::ofstream & the_stream ) {
...
}

I would try to just changing it to

void log ( std::ostream & the_stream ) {
// same body
}

Unless you use file-specific methods (for which I don't see a need), you
should get away with that small change.
Best

Kai-Uwe Bux
Mar 6 '07 #4

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:es********@murdoch.acc.Virginia.EDU...
StephQ wrote:
>On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>>StephQ wrote:
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

What about returning a std::string? You could send that to cout or a
file
using <<. So it seems to fit the description.

Alternatively, you could also make your function use an ostream &
instead
of a ofstream &.

Best

Kai-Uwe Bux

See my reply above.

See my reply to that reply.

>I have never used ostream before. Do you have a link to a reference /
tutorial about it and streams in general?

Unfortunately, computer books are incredibly booring to me (with very,
_very_ few exceptions). I don't read them unless I have to look up
something. For that, I usually refer to the C++ standard. However, it
appears to be general consensus in this forum that this is not a good
recommendation.

However, for the case you mentioned, I take it that you have some function

void log ( std::ofstream & the_stream ) {
...
}

I would try to just changing it to

void log ( std::ostream & the_stream ) {
// same body
}

Unless you use file-specific methods (for which I don't see a need), you
should get away with that small change.
Umm.. I don't see a change. I think you meant to change it to:

std::ostream& log ( std::ostream& the_stream ) {
// same body
return the_stream;
}

didnt' you?

>

Best

Kai-Uwe Bux

Mar 6 '07 #5
Jim Langston wrote:
>
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:es********@murdoch.acc.Virginia.EDU...
>StephQ wrote:
>>On Mar 6, 1:39 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
StephQ wrote:
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

What about returning a std::string? You could send that to cout or a
file
using <<. So it seems to fit the description.

Alternatively, you could also make your function use an ostream &
instead
of a ofstream &.

Best

Kai-Uwe Bux

See my reply above.

See my reply to that reply.

>>I have never used ostream before. Do you have a link to a reference /
tutorial about it and streams in general?

Unfortunately, computer books are incredibly booring to me (with very,
_very_ few exceptions). I don't read them unless I have to look up
something. For that, I usually refer to the C++ standard. However, it
appears to be general consensus in this forum that this is not a good
recommendation.

However, for the case you mentioned, I take it that you have some
function

void log ( std::ofstream & the_stream ) {
...
}

I would try to just changing it to

void log ( std::ostream & the_stream ) {
// same body
}

Unless you use file-specific methods (for which I don't see a need), you
should get away with that small change.

Umm.. I don't see a change. I think you meant to change it to:

std::ostream& log ( std::ostream& the_stream ) {
// same body
return the_stream;
}

didnt' you?
No, I did not. Rereading what I wrote, I find that I changed exactly what I
meant to change. You just missed it. Hint: I removed just one letter!
Best

Kai-Uwe Bux
Mar 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
23498
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is...
2
3161
by: Claus - Arcolutions | last post by:
I got a word document as a stream, and I want to get the text from the word document. But I cant seem to find anything to use for that purpose. The "Microsoft office ?.object" com reference, only...
1
2830
by: Michael Stock | last post by:
Hi all, hopefully someone can tell me how to do this for a web server in C#: We have images in a folder you can't access over the net. So everytime we need to display an image from this folder,...
2
15460
by: CVerma | last post by:
I'm using an html input control (System.web.UI.HTMLControls.HTMLInputFile) to upload files such as msword, excel, jpg, and pdf. I have the encType property set in the form:...
7
13338
by: semedao | last post by:
Hi, I am using cryptostream on both sides on tcp connection that pass data. I am also use asyc socket , so , the data that recieved in the callback method not always have the length of the buffer...
5
3209
by: chandanlinster | last post by:
When I was reading the man-page of fflush, I came across this statement --- "The function fflush forces a write of all user-space buffered data for the given output or update stream via the...
3
301
by: StephQ | last post by:
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...
4
3741
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, (sorry to begin with Java in a Python list ;-) in Java, when I want to pass input to a function, I pass "InputStream", which is a base class of any input stream. In Python, I found...
0
2177
by: RobUK | last post by:
We have Apache on the local network managing several websites that are accessed using virtual domains (configured in the local machine's hosts file, eg. 192.168.1.10 points to dev.somedomain.com). ...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.