The document is archived and information here might be outdated


Read from buffer only.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using basic_istream::_Readsome_s instead.

streamsize readsome(
   char_type *_Str, 
   streamsize _Count



The array in which to read the characters.


The number of characters to read.

The count of items in the buffer.

The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. If good is false, the function calls setstate(failbit). Otherwise, it assigns the value of rdbuf->in_avail to N. If N < 0, the function calls setstate(eofbit). Otherwise, it replaces the value stored in N with the smaller of _Count and N, and then calls read(_Str, N). In any case, the function returns gcount.

// basic_istream_readsome.cpp
// compile with: /EHsc /W3
#include <iostream>
using namespace std;

int main( )
   char c[10];
   int count = 5;

   cout << "Type 'abcdefgh': ";

   // Can read from buffer or console
   // Note: cin::read is potentially unsafe, consider
   // using cin::_Read_s instead.[0], 2);

   // Can only read from buffer, not from console
   // Note: cin::readsome is potentially unsafe, consider
   // using cin::_Readsome_s instead.
   cin.readsome(&c[0], count);  // C4996
   c[count] = 0;
   cout << c << endl;


Type 'abcdefgh': abcdefgh

Header: <istream>

© 2016 Microsoft