A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_basic_ios_readsome.htm below:

C++ istream::readsome() function

C++ istream::readsome() function

The C++ std::istream::readsome() function is used to read the specific number of characters from a input stream into a buffer, without blocking. It reads up to the specified limit or the number of available characters, whichever is smaller.

Unlike read(), which waits to fill the buffer completely, readsome() returns immediately with the available data.

Syntax

Following is the syntax for std::istream::readsome() function.

streamsize readsome (char* s, streamsize n);
Parameters Return Value

This function returns the number of characters stored.

Exceptions

If an exception is thrown, the object is in a valid state.

Data races

Modifies the elements in the array pointed by s and the stream object.

Example

Let's look at the following example, where we are going to consider the bsic usage of readsome() function.

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream x("Tp, TutorialsPoint");
    char a[5];
    x.readsome(a, 4);
    a[5] = '\0';
    std::cout << "Result : " << a << std::endl;
    return 0;
}
Output

Output of the above code is as follows −

Result : Tp, 
Example

Consider the following example, where we are going to read partial data from std::istringstream.

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream a("Hello Namaste");
    char b[6];
    a.readsome(b, sizeof(b) - 1);
    b[a.gcount()] = '\0';
    std::cout << "First read: " << b << std::endl;
    a.readsome(b, sizeof(b) - 1);
    b[a.gcount()] = '\0';
    std::cout << "Second read: " << b << std::endl;
    return 0;
}
Output

Following is the output of the above code −

First read: Hello
Second read:  Nama

istream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4