Converting ostream into standard string

后端 未结 3 835
温柔的废话
温柔的废话 2020-12-07 16:16

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 Tex         


        
相关标签:
3条回答
  • 2020-12-07 16:47
         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.

    0 讨论(0)
  • 2020-12-07 16:54

    Try std::ostringstream

       std::ostringstream os;
       os<<"Hello world";
       std::string s=os.str();
       const char *p = s.c_str();
    
    0 讨论(0)
  • 2020-12-07 16:58

    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();
    }
    
    0 讨论(0)
提交回复
热议问题