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:

  • n: It represent maximum number of character to be read.
  • a: It is the pointer to array where the extracted characters are stored.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

ksforG

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

mpu

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.