this should be pretty common yet I find it fascinating that I couldn’t find any straight forward solution.
Basically I read in a file over the network into a stringstream. This is the declaration:
std::stringstream membuf(std::ios::in | std::ios::out | std::ios::binary);
Now I have some C library that wants direct access to the read chunk of the memory. How do I get that? Read only access is OK. After the C function is done, I dispose of the memorystream, no need for it.
str() copies the buffer, which seems unnecessary and doubles the memory.
Am I missing something obvious? Maybe a different stl class would work better.
Edit:
Apparently, stringstream is not guaranteed to be stored continuously. What is?
if I use vector<char> how do I get byte buffer?
You can call
str()to get back astd::string. From there you can callc_str()on thestd::stringto get achar*. Note thatc_str()isn’t officially supported for this use, but everyone uses it this way 🙂Edit
This is probably a better solution:
std::istream::read. From the example on that page: