There are two issues with using readsome() the way you do, besides the general comment that it's is completely optional.
The purpose of readsome() is to grab the next n bytes from whatever data were already pulled in from the character source into the stream's internal buffer by the last call to rdbuf()->underflow(). When the stream is constructed, it does not (in this case) attempt to immediately read from the source, its buffer is empty. readsome() has nothing to get you.
edit: technically, in this case it falls back to (also completely optional) showmanyc(), to find out how much data is available in the data source, but in this implementation showmanyc happens to return "not sure" (zero).
Even if you prime the stream buffer (by a regular read() or get(), etc), readsome will not set any stream flags when it reaches the end of the stream's buffer: it doesn't know if there's more data available in the source (because showmanyc() isn't telling).
The following works on my tests:
#include <iostream>
#include <complex>
#include <iomanip>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
int main()
{
namespace boostio = boost::iostreams;
char arr[10] = "example";
boostio::stream<boostio::array_source> memStream(arr);
char c;
while(memStream.get(c)) // prime the buffer by reading 1 char
{
std::cout << c;
char tst[2];
while(memStream.readsome(tst, 2) > 0) // read some more
for(int n = 0; n < memStream.gcount(); ++n)
std::cout << tst[n];
}
std::cout << '\n';
}
On closer look, iostreams is being smart about this, and when I read the first char, it makes the internal pointers of streambuffer point directly into the array, so everything that remains to be read can be obtained from readsome() in this case.
Now, as for operator>>, memStream >> setw(2) >> tst; works for me perfectly fine (I hope you remembered about setw when using >> on an array!!), despite the use of the erroneous loop condition "while(!stream.eof())`. You would need to provide a test case that demonstrates the issue you've encountered with operator>>
memStreamis initialized with an uninitializedtst[2]array - this could be a source of the unending loop. Declaring and assigning some meaningful value to the array may help. - damienheofas a loop termination condition. There are plenty of reasons for failure that are not EOF. See latedev.wordpress.com/2012/12/04/all-about-eof - Billy ONeal