473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

streams and functions

Hi,

I have written an app that uses streams in a very standard way. I make
use of some legacy code something like this;
// legacy operator<< , standard enough.
ostream& legacy_function::operator<<(args);
cout << legacy_function(args) << endl;
legacy_function(args) simply takes the args and does some fancy
parsing etc and returns a ostream object in the usual manner. With
this code in dev, I get all my info on stdout, no problemo.

However, now that I have everything up and running I have to log to a
file instead of stdout, which was ok for development and debugging
purposes.

I am obliged to use another 3rd party api (again!) which takes the
form;

void log_message(const char*);

This will automatially log my char* to a pre-configured logfile.

However I am not sure how I can get from the ostream returned from

ostream& legacy_function::operator<<(args);

to the char* that I need to pass to log_message(const char*).
Do I simply create an instance (something like)
std::ostream output(legacy_function::operator<<(args));
const char* the_char_star_im_after(output.some_method);
I hope I've explained that correctly. I haven't used streams much
(well apart from the obvious use, so forgive me if this is blindingly
obvious).

thanks a million

GrahamO
Jul 22 '05 #1
8 1239

"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
Hi,

I have written an app that uses streams in a very standard way. I make
use of some legacy code something like this;
// legacy operator<< , standard enough.
ostream& legacy_function::operator<<(args);
cout << legacy_function(args) << endl;
legacy_function(args) simply takes the args and does some fancy
parsing etc and returns a ostream object in the usual manner. With
this code in dev, I get all my info on stdout, no problemo.
I'm surprised that compiles, you seem to be doing this

cout << another_stream;

which is not standard C++ I think.

However, now that I have everything up and running I have to log to a
file instead of stdout, which was ok for development and debugging
purposes.

I am obliged to use another 3rd party api (again!) which takes the
form;

void log_message(const char*);

This will automatially log my char* to a pre-configured logfile.

However I am not sure how I can get from the ostream returned from

ostream& legacy_function::operator<<(args);

to the char* that I need to pass to log_message(const char*).
Do I simply create an instance (something like)
std::ostream output(legacy_function::operator<<(args));
const char* the_char_star_im_after(output.some_method);
I hope I've explained that correctly. I haven't used streams much
(well apart from the obvious use, so forgive me if this is blindingly
obvious).


I think maybe you are looking for std::ostringstream.

Assuming you can get you output into an ostringstream (called buffer say)
then

const char* the_char_star_im_after = buf.str().c_str();

john

Jul 22 '05 #2
gr************@hotmail.com (grahamo) wrote in message news:<79**************************@posting.google. com>...

[ ... ]
However, now that I have everything up and running I have to log to a
file instead of stdout, which was ok for development and debugging
purposes.

I am obliged to use another 3rd party api (again!) which takes the
form;

void log_message(const char*);

This will automatially log my char* to a pre-configured logfile.

However I am not sure how I can get from the ostream returned from

ostream& legacy_function::operator<<(args);

to the char* that I need to pass to log_message(const char*).


Create an instance of an ostringstream:

std::ostringstream s;

s << whatever;

log_message(whatever.str().c_str());

A bit clumsy, but effective.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3
perfect, thanks a lot for that guys. I was a tad concerned for a moment there,
The compiler (Forte6 on Solaris) barfed on

std::ostringstream output;

but luckily I found this....

std::ostrstream output;
which works. Which one is standard? I'm sure theres a bit of history to it.

thanks again

GrahamO

jc*****@taeus.com (Jerry Coffin) wrote in message news:<b2*************************@posting.google.c om>...
gr************@hotmail.com (grahamo) wrote in message news:<79**************************@posting.google. com>...

[ ... ]
However, now that I have everything up and running I have to log to a
file instead of stdout, which was ok for development and debugging
purposes.

I am obliged to use another 3rd party api (again!) which takes the
form;

void log_message(const char*);

This will automatially log my char* to a pre-configured logfile.

However I am not sure how I can get from the ostream returned from

ostream& legacy_function::operator<<(args);

to the char* that I need to pass to log_message(const char*).


Create an instance of an ostringstream:

std::ostringstream s;

s << whatever;

log_message(whatever.str().c_str());

A bit clumsy, but effective.

Jul 22 '05 #4
grahamo posted:
perfect, thanks a lot for that guys. I was a tad concerned for a moment
there,
The compiler (Forte6 on Solaris) barfed on

std::ostringstream output;

but luckily I found this....

std::ostrstream output;
which works. Which one is standard? I'm sure theres a bit of history to
it.

thanks again

GrahamO

jc*****@taeus.com (Jerry Coffin) wrote in message
news:<b2*************************@posting.google.c om>...
gr************@hotmail.com (grahamo) wrote in message
news:<79**************************@posting.google. com>...

[ ... ]
> However, now that I have everything up and running I have to log to
> a file instead of stdout, which was ok for development and debugging
> purposes.
>
> I am obliged to use another 3rd party api (again!) which takes the
> form;
>
> void log_message(const char*);
>
> This will automatially log my char* to a pre-configured logfile.
>
> However I am not sure how I can get from the ostream returned from
>
> ostream& legacy_function::operator<<(args);
>
> to the char* that I need to pass to log_message(const char*).


Create an instance of an ostringstream:

std::ostringstream s;

s << whatever;

log_message(whatever.str().c_str());

A bit clumsy, but effective.

"std::ostringstream" is standard.

And conversely I belive the header file is "ostrstream", not sure though...

-JKop

Jul 22 '05 #5

"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...
perfect, thanks a lot for that guys. I was a tad concerned for a moment there,

The compiler (Forte6 on Solaris) barfed on

std::ostringstream output;

but luckily I found this....

std::ostrstream output;
which works. Which one is standard? I'm sure theres a bit of history to it.
thanks again

GrahamO


ostrstream is historical and deprecated, but still standard. The header file
is <strstream>. The big issue with it is that /you/ must delete[] the
returned char* pointer after calling str(). Also you are responsible for
ensuring that the string is null terminated, the manipulator std::ends is
provided for this purpose.

ostringstream is standard. The header file is <sstream>.

Use ostringstream if you possibly can.

john
Jul 22 '05 #6
grahamo wrote:
which works. Which one is standard? I'm sure theres a bit of history to it.


They are both standard. stringstream is in <sstream> and stores it's
data in a std::string. This is probably the safer and easier one to
use. strstream is officially deprecated (it predates the standard) is
in the header <strstream> and stores it's data into a char buffer pointed
to by a char*. It obviously has a lot more issues with buffer management.

The one time it is useful is if you have data in already in a char array
you wish to read via a stream. The fact that you can feed a strstream
the buffer pointer directly is a slight improvement in that with a stringstream
you'd have to copy it to a string first. Buffer management in that case
isn't an issue so much as you already have it allocated.

On output, stringstreams win hands down. If you need the char*, just call
the .c_str() or data() member on the string.
Jul 22 '05 #7
Ah thanks for that. It adds up... when using strstream I saw the leak
happening (was in the process of throwing purify at the code when I
read this reponse and previous one). I wasn't calling delete, hence
the leak. Also, I was including the wrong header, hence the
compilation error. I'll go with the reccommended
sstream/std::ostringstream approach.

thanks much for your help. Works a treat now :)
GrahamO

They are both standard. stringstream is in <sstream> and stores it's
data in a std::string. This is probably the safer and easier one to
use. strstream is officially deprecated (it predates the standard) is
in the header <strstream> and stores it's data into a char buffer pointed
to by a char*. It obviously has a lot more issues with buffer management.

The one time it is useful is if you have data in already in a char array
you wish to read via a stream. The fact that you can feed a strstream
the buffer pointer directly is a slight improvement in that with a stringstream
you'd have to copy it to a string first. Buffer management in that case
isn't an issue so much as you already have it allocated.

On output, stringstreams win hands down. If you need the char*, just call
the .c_str() or data() member on the string.

Jul 22 '05 #8
meant to say, wasn't calling "delete[]" in previous post.

thanks again

G

which works. Which one is standard? I'm sure theres a bit of history to it.


They are both standard. stringstream is in <sstream> and stores it's
data in a std::string. This is probably the safer and easier one to
use. strstream is officially deprecated (it predates the standard) is
in the header <strstream> and stores it's data into a char buffer pointed
to by a char*. It obviously has a lot more issues with buffer management.

The one time it is useful is if you have data in already in a char array
you wish to read via a stream. The fact that you can feed a strstream
the buffer pointer directly is a slight improvement in that with a stringstream
you'd have to copy it to a string first. Buffer management in that case
isn't an issue so much as you already have it allocated.

On output, stringstreams win hands down. If you need the char*, just call
the .c_str() or data() member on the string.

Jul 22 '05 #9

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

Similar topics

7
by: Lasse Skyum | last post by:
I would like to implement my own i/o streams for reading from my own dataformat. Is it possible to derive one or more classes from istream,ostream or iostream to do this? When member function would...
3
by: Michael Shestero | last post by:
Hello. I have three question about ofstream and ifstream. 1. I have a function that writes some text-output into ofstream. The reference to that ofstream it got as a parameter. I have no...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
9
by: Mantorok Redgormor | last post by:
7.19.3p7 "At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
4
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) ...
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
1
by: beatTheDevil | last post by:
Hello all, I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents. For...
6
by: Ioannis Papadopoulos | last post by:
I would like to extend the functionality of all streams in C++ so I can do some fancy stuff like redirecting the streams on the fly. I don't want to reimplement the whole stream support in C++...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.