Udostępnij za pośrednictwem


basic_istream::readsome

Reads the specified number of character values.

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 readsome stores the characters it reads.

  • count
    The number of characters to read.

Return Value

The number of characters actually read, gcount.

Remarks

This unformatted input function extracts up to count elements from the input stream and stores them in the array str.

This function does not block. That is, it does not wait for input, it just reads whatever data is available.

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': ";

   // cin.read blocks until user types input.
   // Note: cin::read is potentially unsafe, consider
   // using cin::_Read_s instead.
   cin.read(&c[0], 2);

   // Note: cin::readsome is potentially unsafe, consider
   // using cin::_Readsome_s instead.
   int n = cin.readsome(&c[0], count);  // C4996
   c[n] = 0;
   cout << n << " characters read" << endl;
   cout << c << endl;
}

Input

abcdefgh

Sample Output

Type 'abcdefgh': abcdefgh
5 characters read
cdefg

Requirements

Header: <istream>

Namespace: std

See Also

Reference

basic_istream Class

iostream Programming

iostreams Conventions

Other Resources

basic_istream Members

Change History

Date

History

Reason

October 2008

Revised.

Customer feedback.