It's like readsome isn't even reading. Returns 0 and doesn't read any chars. What is wrong here?
#include <fstream>
#include <iostream>
int main ()
{
std::fstream stream("list.cpp", std::ios::in);
if (stream.good() || !stream.bad() || stream.is_open()) {
std::cout << "Well, stream looks good." << std::endl;
char justOneChar = 'L';
auto ssize = stream.readsome(&justOneChar, 1);
std::cout << ssize << " : " << justOneChar << std::endl;
}
return -1;
}
Output:
Well, stream looks good. 0 : L
auto ssize = stream.readsome(&justOneChar, 1);
1
is the maximum number of characters to read. If the streams internal buffers are empty when you call it, you'll get back zero as a return value.
The following quote (with my bold) shows this aspect:
streamsize readsome (char* s, streamsize n);
Extracts up to
n
characters from the stream and stores them in the array pointed bys
, stopping as soon as the internal buffer kept by the associated stream buffer object (if any) runs out of characters, even if the end-of-file has not yet been reached.The function is meant to be used to read data from certain types of asynchronous sources that may eventually wait for more characters, since it stops extracting characters as soon as the internal buffer is exhausted, avoiding potential delays.
It's basically a way to get as many characters as are available (subject to your specified limit) without having to wait for the stream to provide more.