C++ std::istream::readsome() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, readsome(buffer, count) reads up to count characters that are already available in the stream buffer. It does not wait for more data to arrive.

It returns how many characters it actually copied. With a string stream, every character is available, so it works like a gentle read() that never fails at the end. Include <iostream> to use it.

This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call readsome on an input stream. The returned number tells you how many characters you got.

C++
char buf[10];
std::streamsize n = in.readsome(buf, 10);
CallWhat it does
in.readsome(buf, 4)Copies up to four available characters and returns the number copied.
while ((n = in.readsome(buf, 4)) > 0)Keeps reading until nothing is available.
in.readsome(buf, 10) == 0True when there is nothing available at the moment.

How much is "available" depends on the stream. For std::cin on many systems it can be 0 even when you typed something. Use read() or getline() for normal input.

Library declarations
C++
streamsize readsome(char_type* s, streamsize count);

Parameters

ParameterDescription
sRequired. A buffer that can hold at least count characters.
countRequired. The largest number of characters to copy.

Return Value

Return ValueDescription
std::streamsizeThe number of characters copied. It is 0 when nothing is available.

C++ Examples

Example 1 — Read the Available Text in Pieces

Ask for four characters, then for ten. The second call returns only what is left.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("abcdef");
    char buf[11];

    std::streamsize n = in.readsome(buf, 4);
    buf[n] = '\0';
    std::cout << "first:  " << n << " (" << buf << ")\n";

    n = in.readsome(buf, 10);
    buf[n] = '\0';
    std::cout << "second: " << n << " (" << buf << ")\n";
    return 0;
}

How It Works

  1. The stream holds six characters, all available at once.
  2. The first call asks for four and gets abcd. The returned number 4 is used to place the end marker.
  3. The second call asks for ten, but only ef is left, so it returns 2 instead of failing.

Example 2 — Loop Until readsome() Returns Zero

Collect all text in chunks of three.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("one two");
    char buf[4];
    std::streamsize n = 0;

    while ((n = in.readsome(buf, 3)) > 0) {
        buf[n] = '\0';
        std::cout << "[" << buf << "]\n";
    }
    return 0;
}

How It Works

  1. readsome(buf, 3) copies up to three characters each time and returns how many it copied.
  2. The seven characters of one two split into one, tw (with the leading space), and o.
  3. After o nothing is available, the call returns 0, and the loop ends.

Example 3 — readsome() Does Not Fail at the End

Ask for more characters than exist. Compare how readsome() and read() react.

C++
#include <iostream>
#include <sstream>

int main() {
    char buf[10];

    std::istringstream a("abc");
    std::streamsize n1 = a.readsome(buf, 10);
    std::cout << "readsome got " << n1 << ", fail: " << (a.fail() ? "yes" : "no") << '\n';

    std::istringstream b("abc");
    b.read(buf, 10);
    std::cout << "read got " << b.gcount() << ", fail: " << (b.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. Both streams hold only abc, and both calls ask for ten characters.
  2. readsome() copies the three available characters and stops quietly. No flag is set, so fail() is no.
  3. read() also copies three, but it wanted ten. It sets eofbit and failbit, so fail() is yes.

Frequently Asked Questions

It reads up to count characters that are already in the stream buffer and returns how many it copied. It does not wait for more input.
read() insists on exactly count characters and fails if there are fewer. readsome() takes only what is available and does not fail for being short.
On many systems cin does not report buffered characters, so nothing counts as available. The result depends on the library, so use getline() for normal keyboard input.
No. Use the returned count to place a \0 yourself if you need a C string.
It does not wait, but it is not a full non-blocking solution. It depends on what the stream buffer already holds.

Next: seekg()

Move the read position in an input stream.

seekg() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful