Visual Studio std::stringstream pubsetbuf does not work

c++, std, stl, visual-studio

Solution

`putsetbuf` only makes sense for `fstream` (technically, for `std::basic_filebuf`), where the buffer and the stream are two different things.

For `stringstream` (technically, `std::basic_stringbuf`) they are one and the same std::string.

If you need a stream that works on a string that's external to it, consider `std::strstream`: or `boost::iostreams::array_sink`

Problem

pubsetbuf member of std::stringbuf is not working at all in Visual Studio 2010! The code: ``` char *FileData = ... ; unsigned long long FileDataLen = ... ; std::stringstream *SS = new std::stringstream(std::stringstream::in | std::stringstream::out); SS->rdbuf()->pubsetbuf( FileData, (std::streamsize)FileDataLen ); ``` pubsetbuf does nothing in Visual Studio!!! Workaround #1: ``` std::stringstream *SS = new std::stringstream( std::string(FileData, (size_type)FileDataLen ) ),std::stringstream::in | std::stringstream::out); ``` Workaround #2: ``` SS->rdbuf()->sputn(FileData, (streamsize)FileDataLen); ``` But both of these workarounds produce unnecessary memory copying. I definitely need a working pubsetbuf member of std::stringbuf.

Original source

Related problems