读取字符值的指定数目。
此方法有可能,不安全,因为它依赖于调用方检查传递的值是正确的。
streamsize readsome(
char_type *str,
streamsize count
);
参数
str
readsome 存储字符而言的数组。count
要读取的字符数。
返回值
字符数实际读取,gcount。
备注
此无格式的输入函数提取到 count 输入流中的元素并将它们存储在数组 str。
此函数不等待输入。 它读取任何数据可用。
示例
// 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;
}
输入
abcdefgh
示例输出
Type 'abcdefgh': abcdefgh
5 characters read
cdefg
要求
页眉: <istream>
命名空间: std