Open In App

basic_istream::readsome() in C++ with Examples

The basic_istream::readsome() used to read the data from the buffer and extracts up to n immediately available characters from the input string. This function returns the number of extracted characters. Below is the syntax for the same:

Header File:



#include<iostream>

Syntax:

streamsize readsome(char_type* a,
                    streamsize n);

Parameters:



Return Value: The std::basic_istream::readsome() returns the number of extracted characters.

Below are the programs to understand the implementation of std::basic_istream::readsome() in a better way:

Program 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;
}

Output:
ksforG

Program 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;
}

Output:
mpu

Reference: http://www.cplusplus.com/reference/istream/basic_istream/readsome/


Article Tags :
C++