8

Legacy code I maintain collects text into an std::ostream. I'd like to convert this to a std::string.

I've found examples of converting std::sstringstream, std::ostringstream etc to std::string but not explicitly std::ostream to std::string.

How can I do that? (Ancient C++ 98 only, no boost please)

jpo38
  • 20,821
  • 10
  • 70
  • 151
Danny
  • 2,482
  • 3
  • 34
  • 48
  • 4
    You can't "collect" anything in a [`std::ostream`](http://en.cppreference.com/w/cpp/io/basic_ostream), it's a base-class whose reference one passes around, and the reference somewhere must be referencing an actual instantiable output stream. So follow the call-chain to see what your `std::ostream` reference actually references, and if it's a `std::ostringstream` then you can easily extract the data from it. If it's something else then you're out of luck and need to use something else to get the output (adding a parallel writing to an *actual* `std::ostringstream` perhaps?). – Some programmer dude Mar 18 '17 at 07:24
  • Thanks! That explains a lot of things. – Danny Mar 19 '17 at 01:33
  • I just can't believe nobody has a solution to this issue: how does one copy the contents from a std::basic_ostream into a std::string? The answer to [this question](https://stackoverflow.com/questions/59473325/no-member-named-str-in-stdbasic-ostreamchar-with-gcc-and-clang-but-no-p) explains the underlying issue I'm having: gcc doesn't provide a `str()` function as part of stringstream while clang and msvc have it. So what do gcc users do to get the string? – taranaki Oct 04 '20 at 19:17

2 Answers2

6
  • If you have a a stringstream or a ostreamstring, object or reference, just use the ostringstream::str() method that does exactly what you want: extract a string. But you know that and you apparently have a ostream object, not a stringstream nor a ostreamstring.

  • If your ostream reference is actually a ostringstream or a stringstream, use ostream::rdbuf to get a streambuf, then use this post to see how to extract a string.

Example:

#include <iostream>
#include <sstream>

std::string toString( std::ostream& str )
{
    std::ostringstream ss;
    ss << str.rdbuf();
    return ss.str();
}

int main() {

    std::stringstream str;
    str << "Hello";

    std::string converted = toString( str );

    std::cout << converted;

    return 0;
}

Live demo: http://cpp.sh/6z6c

  • Finally, as commented by M.M, if your ostream is a ofstream or an iostream, most likely it clears its buffer so you may not be able to extract the full content as proposed above.
Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • I don't think this works; the post you link to refers to an input stream's `rdbuf`. – M.M Mar 18 '17 at 07:46
  • @M.M: Just tested that (see live demo added to my post), it works when `ostream` is actually a `stringstream`, so I d'ont see why it would not work with others (like `ofstream`). – jpo38 Mar 18 '17 at 07:52
  • stringstreams always hold the content in memory, whereas ofstreams don't, I'd expect they clear their buffer once written the current line or whatever – M.M Mar 18 '17 at 08:14
  • @M.M: You are probably right, I commented my post to mention that. Maybe the answer won't work when `ostream` is not actually a `ostringstream`... – jpo38 Mar 18 '17 at 08:18
1

Try this

 std::ostringstream stream;
 stream << "Some Text";
 std::string str =  stream.str();
Abi
  • 724
  • 6
  • 22
  • By the way, OP says `I've found examples of converting std::ostringstream to std::string`, so he knows that... – jpo38 Mar 18 '17 at 08:15