c++ - ostringstream breaks cout? -
i want output content of ostringstream
other stream (for example std::cout
). know can use std::ostringstream::str()
assume has overhead on copying stream contents string , further other stream. found use std::ostringstream::rdbuf()
(comment suggesting has 25 votes). breaks std::cout
shown in output of test program below. doing wrong?
#include <iostream> #include <sstream> int main(int argc, char const *argv[]) { std::ostringstream ss; ss << "some data" << std::endl; std::cerr << std::cout << std::endl; std::cout << "start" << std::endl; std::cout << ss.str(); std::cout << ss.rdbuf(); std::cout << "end" << std::endl; std::cerr << std::cout << std::endl; return 0; }
results in:
0x6013b8 start data 0
your problem rdbuf()
buffer ostringstream
is, might expect name, write only (the ostringstream
returns string through str()
method). can't read data out of through buffer pointer.
change ostringstream
stringstream
, should work fine.
Comments
Post a Comment