Re: streams and functions

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 10/22/04


Date: Thu, 21 Oct 2004 23:10:33 +0100


"grahamo" <graham_walsh50@hotmail.com> wrote in message
news:79528aa8.0410210736.69a893eb@posting.google.com...
> 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