共用方式為


basic_istream::readsome

讀取字元值的指定數目。

這個方法是可能不安全的,因為,它便相依於該呼叫端檢查傳遞的值是正確的。

streamsize readsome(
    char_type *str,
    streamsize count
);

參數

  • str
    readsome 儲存字元其陣列讀取。

  • count
    要讀取的字元數。

傳回值

字元數實際讀取, gcount

備註

這個未格式化的輸入函式在陣列 str捕捉 count 輸入資料流的項目並儲存它們。

這個函式不等待輸入。 它會讀取任何資料可供使用。

範例

// 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

請參閱

參考

basic_istream Class

iostream 程式設計

iostreams 慣例