Converting ostream into standard string

Stephen Diehl picture Stephen Diehl · Aug 18, 2010 · Viewed 92.7k times · Source

I am very new to the C++ STL, so this may be trivial. I have a ostream variable with some text in it.

ostream* pout;
(*pout) << "Some Text";

Is there a way to extract the stream and store it in a string of type char*?

Answer

Foo picture Foo · Jul 22, 2013

The question was on ostream to string, not ostringstream to string.

For those interested in having the actual question answered (specific to ostream), try this:

void someFunc(std::ostream out)
{
    std::stringstream ss;
    ss << out.rdbuf();
    std::string myString = ss.str();
}