10

I like to use std::ostrstream to format text but not print it to stdout but instead write it into an std::string (by accessing the std::ostrstream::str() member). Apparently this is deprecated now. So, how am I supposed to write formatted objects to a string, with the same convenience as when writing to a stream?

bitmask
  • 32,434
  • 14
  • 99
  • 159

5 Answers5

18

You could use std::ostringstream. Similarly, instead of std::istrstream you should use std::istringstream. You need to include the <sstream> header for these classes.

You could also see this question which explains why strstream was deprecated.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • I am baffled by the downvote! Does anyone see something wrong in there? – Shahbaz Jun 01 '12 at 13:01
  • 2
    You got an upvote from me, but I would guess the downvote was because you linked to cplusplus.com, which is terrible and full of bad style and some outright errors. – Jonathan Wakely Jun 01 '12 at 13:08
  • @JonathanWakely, I am aware that cplusplus.com sucks. I try to avoid it myself always. Do you know a reliable website I can include? The problem is that searched on google **always** brings that website up first. When you go further down, you see MSDN (which I wouldn't even consider for an instance) and then you are out of reference sites and into Q&A sites such as stackoverflow. – Shahbaz Jun 01 '12 at 13:12
  • I used to use the dinkumware manuals but last time I checked they'd gone. – Jonathan Wakely Jun 01 '12 at 13:20
  • @JonathanWakely, you see? We need a good reference online, but I can't find any. – Shahbaz Jun 01 '12 at 13:26
  • I'm under the impression that cppreference.com (which I linked to in the OP) is better than cplusplus.com but I've nothing to back that up, unfortunately. – bitmask Jun 01 '12 at 14:20
  • @bitmask, I changed the link to cppreference.com. I guess good or bad, it can't be as bad as cplusplus.com – Shahbaz Jun 01 '12 at 14:29
3

As others have already said, std::ostringstream is the replacement.

It's more convenient (and safer) than std::ostrstream because it manages all memory automatically so you don't need to call freeze(false) to ensure the memory gets freed when you're finished with it.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
1

You should use std::stringstream. Also, see boost::lexical_cast.

std::stringstream supports both << and >>. std::ostringstream only supports <<, and std::istringstream only supports >>. Often I find it convenient to be able to use both operators.

robert
  • 33,242
  • 8
  • 53
  • 74
0

You can also use boost::format. Then you can do things like:

int a = 1;
std::string b("foo");
std::string s = boost::str(
    boost::format("some text, some vars=%1%, %2%, %1%") % a % b % a);

Then s would contain "some text, some vars=1, foo, 1".

This is, in my opinion, more convenient in some cases than using operator <<.

As a reference, I also include the format specification.

betabandido
  • 18,946
  • 11
  • 62
  • 76
  • 1
    You want to stop using '%d' and other type specific stuff as this will lead to the same problems as sprintf(). Prefer the index specific identifiers '%1'. – Martin York Jun 01 '12 at 11:58
  • @LokiAstari thanks for that! I didn't know about it. I'll update my answer accordingly. But what if you just want to show floating point numbers with two decimals? Is there any other way than using `%.02f`? – betabandido Jun 01 '12 at 12:01
  • http://www.boost.org/doc/libs/1_49_0/libs/format/doc/format.html#printf_directives "%1$.02f" – Martin York Jun 01 '12 at 12:03
0

With C++20 you can use std::format

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217