fork(4) download
  1. //http://stackoverflow.com/questions/6010864/copying-stringstream/6010930#6010930
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. stringstream ss;
  10. streambuf *coutbuf = cout.rdbuf();
  11. cout.rdbuf(ss.rdbuf());
  12. cout << "this goes to the stringstream" << endl;
  13. string s = ss.str();
  14. cout.rdbuf(coutbuf);
  15. cout << "after cour.rdbuf : " << s;
  16. return 0;
  17. }
  18.  
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
after cour.rdbuf : this goes to the stringstream