1

The next code returns an empty string in ss:

#include <string>       
#include <iostream>     
#include <sstream>      

int main () {
  std::ostringstream oss;
  oss << "Text";

  std::stringstream ss;
  ss.basic_ios<char>::rdbuf(oss.rdbuf());
  std::cout << ss.str() << "\n";
  return 0;
}

How can I return from ss the text introduced in oss? I'm mainly interested in converting std::ostringstream into std::stringstream.

Medical physicist
  • 2,510
  • 4
  • 34
  • 51
  • 1
    Why can't you use `oss.str()` instead? What is the actual problem you need to solve? Right now this is too much of an [XY problem](http://xyproblem.info/). – Some programmer dude Apr 16 '20 at 13:12
  • @Someprogrammerdude The real problem is too long, but it will be solved if I find the solution to this one. – Medical physicist Apr 16 '20 at 13:15
  • @Medicalphysicist read again what is the nature of `XY problem` (length has nothing to that), you are asking how to fix your invalid solution not how to properly resolve actual problem. – Marek R Apr 16 '20 at 13:22
  • If you have stuff in ss, and you are trying to concat ss with oss you can just use `std::cout << ss.str() << oss.str() << "\n";` This is more efficient then trying to combine them into a single object just to print. If you need a single string then just return `ss.str() + oss.str()` – NathanOliver Apr 16 '20 at 13:25
  • Lets take a step back and look at what you are asking to see why you can't do this. `oss` is an **output** stream. When you do `ss.basic_ios::rdbuf(oss.rdbuf());` (or the shorthand of `ss << oss.rdbuf()`) you are asking to **read** from `oss`. That's not going to work as `oss` is for **writing** to, not reading from. – NathanOliver Apr 16 '20 at 13:29
  • Thanks, @NathanOliver. Is there any way to convert std::ostringstream into std::stringstream without using .str()? – Medical physicist Apr 16 '20 at 13:31
  • 2
    AFAIK, No. `oss` is for writing to, not reading from. – NathanOliver Apr 16 '20 at 13:32
  • @Medicalphysicist Didn't my answer do what you wanted? – Ted Lyngmo Apr 17 '20 at 22:06
  • @TedLyngmo Thank you for your answer, it was very useful. But the most helpful solution was the comment of NathanOliver. – Medical physicist Apr 19 '20 at 11:32
  • @Medicalphysicist You are welcome. How did you manage to use Nathans comment to "_return from ss the text introduced in oss_"? You should write an answer to your question yourself and describe how so that others may make use of it. – Ted Lyngmo Apr 19 '20 at 11:35

2 Answers2

1

You could make use of the protected std::streambuf::swap member function that exchanges the contents of the stream buffer with those of another

To get access to it, you'll need a derived class:

#include <iostream>
#include <sstream>

struct swapper : std::streambuf {
    using std::streambuf::streambuf;
    void swap(std::streambuf& rhs) {         // public proxy for protected swap
        std::streambuf::swap(rhs);
    }
};

// casting
void swapbuf(std::ostream& a, std::ostream& b) {
    static_cast<swapper*>(a.rdbuf())->swap(*b.rdbuf());
}

int main () {
    std::ostringstream oss;
    oss << "Text";
    std::stringstream ss;
    swapbuf(oss, ss);

    std::cout << "ss:  " << ss.str() << "\n";   // prints Text
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

Following comments from @NathanOliver, I decided to convert std::ostringstream into std::stringstream by using str():

#include <string>       
#include <iostream>     
#include <sstream>      

int main () {
  std::ostringstream oss;
  oss << "Text";

  std::stringstream ss;
  ss << oss.str();
  std::cout << ss.str() << "\n";
  return 0;
}
Medical physicist
  • 2,510
  • 4
  • 34
  • 51