次の方法で共有


basic_istream::readsome

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
);

Parameters

  • _Str
    The array in which to read the characters.

  • _Count
    The number of characters to read.

Return Value

The count of items in the buffer.

Remarks

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.

Example

// 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.
   cin.read(&c[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;
}

Input

abcdefgh

Sample Output

Type 'abcdefgh': abcdefgh
cdefg

Requirements

Header: <istream>

Namespace: std

See Also

Concepts

basic_istream Class

basic_istream Members

iostream Programming

iostreams Conventions