Udostępnij za pośrednictwem


basic_istream::read

Reads a specified number of characters from the stream and stores them in an array.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct.

basic_istream<Elem, Tr>& read(
    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 stream (*this).

Remarks

The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. Extraction stops early on end of file, in which case the function calls setstate(failbit). In any case, it returns *this.

Example

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

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

    cout << "Type 'abcde': ";

    // Note: cin::read is potentially unsafe, consider
    // using cin::_Read_s instead.
    cin.read(&c[0], count);
    c[count] = 0;

    cout << c << endl;
}
  abcde

FakePre-92eb65a5f2dc48a9bf0dffe5fbd48039-0cc8af99bb53450ea6f6b655612c1660

Requirements

Header: <istream>

Namespace: std

See Also

Reference

basic_istream Class

iostream Programming

iostreams Conventions

Other Resources

basic_istream Members