1

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.

prehawk
  • 195
  • 12
  • Possible duplicate of [stringstream->rdbuf()->pubsetbuf is not setting the buffer](http://stackoverflow.com/questions/12481463/stringstream-rdbuf-pubsetbuf-is-not-setting-the-buffer) – Ciro Santilli OurBigBook.com Mar 30 '17 at 16:25

1 Answers1

0

ss is a different object than the "buffer" it is not a pointer to "buffer". When you do "pubsetbuf" you copy the contents of buffer into "ss". You might want to try using "ss<<buffer" instead anyway, it will be easier.

Axel
  • 13,939
  • 5
  • 50
  • 79
in need of help
  • 1,606
  • 14
  • 27
  • I have looked up some posts on this site, calling ss.rdbuf()->pubsetbuf() is kind of a common way of setting an existing buffer to a stringstream. And you said it's not. Now I am really confuse about it :) – prehawk Mar 06 '14 at 08:44
  • "setting an existing buffer to a stringstream" means that it will copy the contents into the stringstream, you will need to call that function again after changing buffer. – in need of help Mar 06 '14 at 08:46
  • Since this function is copying data, is it not efficient enough than simply using operator<< ? – prehawk Mar 06 '14 at 08:50
  • That is not what I meant, both will copy the contents. stringstream does not hold a pointer to an external buffer and cannot be manipulated by changing a different buffer. – in need of help Mar 06 '14 at 08:52
  • I have edit the post. In my case, i want to directly set my buffer. And I want this ss object be aware of that. – prehawk Mar 06 '14 at 09:05
  • putting an additional pubsetbuf() call doesn't work :) – prehawk Mar 06 '14 at 09:07
  • `sprintf(buffer, "efgh"); ss.rdbuf()->pubsetbuf(buffer, sizeof buffer);` – in need of help Mar 06 '14 at 09:41
  • Anyway you do it when you change the value of "buffer" the value of ss will not change. – in need of help Mar 06 '14 at 09:42
  • Is there any solutions? I can't change the 'recv' system call, thus I could only change the content by changing the buffer. – prehawk Mar 06 '14 at 10:33
  • stringstream is a C++ object which can't anyway be used in system calls. You can only use "char*" in system calls. why do you need a "stringstream" in the first place? – in need of help Mar 06 '14 at 11:08
  • I think using stringstream will be easier for parsing some string like http header:) – prehawk Mar 06 '14 at 11:17