c++ - I am using stringstream with my own buffer, but its str() method doesn't related to my buffer? -
i want use own buffer zone stringstream, changing buffer twice won't expect 2 output code shown follow.
std::stringstream ss; char buffer[10]; memset(buffer, '\0', sizeof buffer); ss.rdbuf()->pubsetbuf(buffer, sizeof buffer); sprintf(buffer, "abcd"); std::cout << ss.str() << std::flush; ss.rdbuf()->pubsetbuf(buffer, sizeof buffer); sprintf(buffer, "efgh"); std::cout << ss.str() << std::flush;
and result is:
abcd
after setting buffer "efgh", ss.str() doesn't show me new content, why that?
and reason why want directly set internal buffer should system call recv.
now found out event if change buffer totally using pubsetbuf in second calling, not change @ all, remaining previous contents.
ss different object "buffer" not pointer "buffer". when "pubsetbuf" copy contents of buffer "ss". might want try using "ss<<buffer" instead anyway, easier.
Comments
Post a Comment