当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。