I want to use my own buffer zone for the stringstream, but changing the buffer twice won't expect two output just as the 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 the result is:
abcd
After I setting the buffer to "efgh", the ss.str() doesn't show me the new content, why is that?
And the reason why I want to directly set the internal buffer is that it should be a system call like recv.
Now I found out that event if I change the buffer totally using pubsetbuf in the second calling, it does not change at all, remaining the previous contents.