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



Relevant Pages

  • Re: stdin and stdout
    ... > I have read about stdin and stdout being standard input and output ... > streams, but I am still having trouble visualizing how they really ... stdout and stderr are output ...
    (comp.lang.c)
  • Re: sys.stdout vs. sys.stderr
    ... C library set up as stdout and stderr. ... the standard error stream is not fully buffered; ... standard input and standard output streams are fully buffered if and only ...
    (comp.lang.python)
  • Re: getting out of LaTeX
    ... And, as the standard states, when there is agreement between the ... The ASCII standard of 1963 already ... 016 0E SO Shift Out ctrl-N ... the idea of files as byte streams. ...
    (sci.lang)
  • Re: NEW_LINE()
    ... I don't recall ever using text streams in my own code. ... standard, then other non-standard extensions (like some kind ... > I/O can still have a record length limit. ... > a line buffer to be written out with a record mark at the end. ...
    (comp.lang.fortran)
  • Re: couple of questions on streams
    ... three text streams are predefined and need not ... > standard error ... > determined not to refer to an interactive device." ...
    (comp.lang.c)