Converting ostream into standard string

106,106

Solution 1

     std::ostringstream stream;
     stream << "Some Text";
     std::string str =  stream.str();
     const char* chr = str.c_str();

And I explain what's going on in the answer to this question, which I wrote not an hour ago.

Solution 2

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();
}

Solution 3

Try std::ostringstream

   std::ostringstream os;
   os<<"Hello world";
   std::string s=os.str();
   const char *p = s.c_str();
Share:
106,106
Stephen Diehl
Author by

Stephen Diehl

I work with Haskell, functional compilers, and type systems. I'm also on Twitter.

Updated on July 22, 2022

Comments

  • Stephen Diehl
    Stephen Diehl almost 2 years

    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*?

  • Stephen Diehl
    Stephen Diehl almost 14 years
    I get the following error message: 'struct std::basic_ostream<char, std::char_traits<char> >' has no member named 'str'
  • Prasoon Saurav
    Prasoon Saurav almost 14 years
    Include the necessary headers .#include <string> and #include <sstream>
  • Vanuan
    Vanuan about 12 years
    It's not ostream. It is ostringstream
  • ArtOfWarfare
    ArtOfWarfare over 11 years
    Note that ostringstream << int seems to return a basic_ostream but won't change the type of the lhs? Thus having something like oss << value; return oss.str(); will compile while return (oss << value).str(); will not.
  • moodboom
    moodboom over 10 years
    I'll give you points for being a hothead. Alright.. and for the right answer, heh.
  • Matthew James Briggs
    Matthew James Briggs almost 10 years
    This is the correct answer. The answer above, marked as correct, does NOT answer the question.
  • Danny
    Danny about 7 years
    This doesn't answer the question, which was "convert ostream to std:string"
  • James Curran
    James Curran about 7 years
    @Danny It answers the question stated in the text of the message, not the (different) question asked in the title.
  • Danny
    Danny about 7 years
    Weird. From what I see, neither the title nor text of the question contain the word "ostringstream", while your answer does not contain the word "ostream".
  • Brandon
    Brandon almost 5 years
    The parameter should be std::ostream& out because std::ostream isn't copyable.
  • Marc Dirven
    Marc Dirven almost 3 years
    This doesn't answer the question. It's regarding a ostream, not ostringstream.
  • Marc Dirven
    Marc Dirven almost 3 years
    The question isn't about std::ostringstream but about std::ostream. std::ostream doesn't have a method .str().