當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ basic_istream::readsome()用法及代碼示例


basic_istream::readsome()用於從緩衝區讀取數據,並從輸入字符串中提取最多n個立即可用的字符。此函數返回提取的字符數。以下是相同的語法:

頭文件:

#include<iostream>

用法:

streamsize readsome(char_type* a,
                    streamsize n);

參數:

  • n:它代表要讀取的最大字符數。
  • a:它是存儲提取的字符的數組的指針。

返回值:std::basic_istream::readsome()返回提取的字符數。



下麵的程序可以更好地了解std::basic_istream::readsome()的實現:

程序1:

// C++ code for basic_istream::readsome() 
#include <bits/stdc++.h> 
using namespace std; 
  
// main method 
int main() 
{ 
    char gfg[50] = {}; 
    istringstream gfg1("GeeksforGeeks"); 
  
    // reads 'Gee' and 
    // stores in c[0] .. c[2] 
    gfg1.readsome(gfg, 3); 
  
    // reads 'ksforG' and 
    // stores in c[0] .. c[5] 
    gfg1.readsome(gfg, 6); 
    cout << gfg << endl; 
}
輸出:
ksforG

程序2:

// C++ code for basic_istream::readsome() 
#include <bits/stdc++.h> 
using namespace std; 
  
// main method 
int main() 
{ 
    char gfg[50] = {}; 
    istringstream gfg1("Computer"); 
  
    // reads 'Co' and 
    // stores in c[0] .. c[1] 
    gfg1.readsome(gfg, 2); 
  
    // reads 'mpu' and 
    // stores in c[0] .. c[4] 
    gfg1.readsome(gfg, 3); 
    cout << gfg << endl; 
}
輸出:
mpu

參考: http://www.cplusplus.com/reference/istream/basic_istream/readsome/




相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 basic_istream::readsome() in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。