Search code examples
c++stringstreamstreambuf

std::stringstream buffer manipulation


I am putting some data into a stream buf obtained from stringstream

std::stringstream data;
auto buf = data.rdbuf();
buf->sputn(XXX);

What I want is to be able to put some dummy data into this buffer and then at a later time, once I have the correct data, replace the dummy data.

Something on these lines:

auto count = 0;
buf->sputn((unsigned char *)&count, sizeof(count));
for (/*some condition*/)
{
   // Put more data into buffer

   // Keep incrementing count
}

// Put real count at the correct location

I tried using pubseekpos + sputn but it doesnt seem to be working as expected. Any ideas what could be the correct way to do this?


Solution

  • Just use data.seekp(pos); then data.write() - you shouldn't need to stuff around with the buffer at all.