The first example works fine for me
Code:
#include <string>
#include <iostream>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<char> cv {'k','u','r','t' };
stringstream str;
str.rdbuf()->pubsetbuf(&cv[0], 3); // intentionally set the size to only 3
// to see if it works if the string is not 0-terminated
char c;
str.get( c );
string val;
cout << "reading 1 char with get()\n"
<< "'" << c << "' gcount=" << str.gcount() << endl
<< "reading the rest returns \"";
str >> val;
cout << val << "\"" << endl
<< "now gcount() is still supposed to be 1 because it should return\nthe count of the last unformatted input gcount="
<< str.gcount() << endl;
}
my output :
Code:
reading 1 char with get()
'k' gcount=1
reading the rest returns "ur"
now gcount() is still supposed to be 1 because it should return
the count of the last unformatted input gcount=1
The second example is the usual way to initialize the stringstream from a string.
But I guess there is a problem the way you did it.
Code:
ssbuffer.str(&vbuffer[0]);
you call str() with a string that is initialized from a const char *, but the data is possibly not 0-terminated.
Kurt