std::istreamhas a prototype istream& read (char* s, streamsize n), the actual number of bytes read must be obtained by calling istream::gcount(), also the reliability istreamcan be known from ios::good.
std::istream
istream& read (char* s, streamsize n)
istream::gcount()
istream
ios::good
I discussed the implementation of another thread class that I was trying to write with my colleague, where I said that I could follow this design; but he said instead of invoking the gcount user each time, one could read a prototype like this istream& read (char* s, streamsize n, size_t &bytes_read)so that it would go into one call, and the first would be awkward. I could not protect the design choice std. What is the true rationale istream::read?
istream& read (char* s, streamsize n, size_t &bytes_read)
std
istream::read
I suppose, because C ++ usually does not force an interface that everyone might not need. If you need readto accept a parameter that some people do not care about, then it causes additional coding work (declaring an additional int to pass as a parameter). It also always saves read bytes regardless of whether the client cares or not (some clients may just take care that the read fails as indicated by the eof / fail bits).
read
Using a separate method, you remove the interface for various pieces of information that may or may not be needed.
Try the readsome command instead,
streamsize readsome ( char* buf, streamsize num );
buf - , num - , , , .
- .
, :
char buf[BUF_SIZE] streamsize bytesRead; do { bytesRead = instr.readsome( buf, BUF_SIZE ); // do stuff with the bytes you read, if any } while ( bytesRead == BUF_SIZE );
std::istream istream& read (char* s, streamsize n) istream::gcount(), istream ios::good.
istream::read(char* s, streamsize n) ( NULL ) n s. s char, istream::read . , istream, ( , ):
istream::read(char* s, streamsize n)
NULL
n
s
char
unsigned int count; input.read(reinterpret_cast<char*>(&count), sizeof(count)); double* data = new double[count]; for (unsigned int i = 0; i < count; ++i) input.read(reinterpret_cast<char*>(data[i]), sizeof(double));
istream::gcount() , istream::read. , count, , a double, istream::gcount() data.
count
double
data
, , C , . , , , , , , , . , , -.
Cash Cow , . IO , while , . , , , -, IO.
, , while, EOF. , - , , , .
, readsome - .
: readsome ( V++). .