Search code examples
c++stringfunctioniostreamostream

How to convert ostream into a string


In a function i'm passing ostream and wants to convert into a string.

void func (ostream& stream) {
    /* Needs to convert stream into string */
}

Solution

  • void func (ostream& stream) {
        /* Needs to convert stream into string */
        std::stringstream ss;
        ss << stream.rdbuf();
        std::string myString = ss.str();
    }