Topic: Deprecating strstream a mistake?


Author: jelbaum@yahoo.com (Jason Elbaum)
Date: Tue, 26 Nov 2002 15:45:34 +0000 (UTC)
Raw View
Despite all the clear advantages of stringstream over the pre-standard
strstream, I wonder whether deprecating strsteam wasn't a mistake.

Unlike ostrstream, ostringstream does not support formatting text into
a fixed-length character buffer. You can't use it without creating a
dynamically-allocated string object. For efficiency-critical uses,
this is a real drawback. strstream could direct its output to a
character array on the stack.

This seems to me to be basic functionality, which was supported by
sprintf() and ostrstream. Is there no recommended way to do this in
standard C++?

Regards,

Jason Elbaum
jelbaum@yahoo.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 26 Nov 2002 17:53:17 +0000 (UTC)
Raw View
Jason Elbaum wrote:
> This seems to me to be basic functionality, which was supported by
> sprintf() and ostrstream. Is there no recommended way to do this in
> standard C++?

#include <iostream>
#include <stdio.h>

struct obufbuf : public std::streambuf
{
     obufbuf(char *b, char *e) { setp(b, e); }
};

int main()
{
     char buf[100];
     obufbuf b(buf, buf + 100);
     std::ostream o(&b);
     o << "Hello, world (version " << 1.0 << ")" << std::ends;
     printf("%s\n", buf);
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]