Reputation: 89
I have some issue with reading wstringstream data. Code like this:
#include <iostream>
#include <sstream>
int main(int argc, char* argv[]) {
std::wstringstream buf(L"dsadsadsad dsadsadsad sadsadsadsad sa dsadsadsads dasdsadsa");
wchar_t sendbuf[5];
wmemset(sendbuf, 0, 5);
while (buf.read(sendbuf, 5))
{
std::wcout << sendbuf;
wmemset(sendbuf, 0, 5);
}
return 0;
}
but it do not print out the whole data, Why?
Upvotes: 0
Views: 100
Reputation: 2696
The std::wostream::operator<<
takes a wchar_t*
parameter, so it can't know the length of your buffer. You need an extra space in your buffer for the terminating zero.
int main(int argc, char* argv[]) {
std::wstringstream buf(L"dsadsadsad dsadsadsad sadsadsadsad sa dsadsadsads dasdsadsa");
wchar_t sendbuf[6];
wmemset(sendbuf, 0, 6);
while (buf.read(sendbuf, 5))
{
std::wcout << sendbuf;
wmemset(sendbuf, 0, 6);
}
return 0;
}
Upvotes: 2