# std::basic_istream<CharT,Traits>::readsome

> C++ Atlas: `cppatlas.com/w/cpp/io/basic_istream/readsome` | Source: https://en.cppreference.com/w/cpp/io/basic_istream/readsome | Domain: Input/output

Extracts up to count immediately available characters from the input stream. The extracted characters are stored into the character array pointed to by s.

## Official Specification Summary (cppreference)

```cpp
std::streamsize readsome( char_type* s, std::streamsize count );
```

Extracts up to `count` immediately available characters from the input stream. The extracted characters are stored into the character array pointed to by `s`.

Behaves as *UnformattedInputFunction*. After constructing and checking the sentry object,

- If `rdbuf()->in_avail() == -1`, calls `setstate(eofbit)` and extracts no characters.

- If `rdbuf()->in_avail() == 0`, extracts no characters.

- If `rdbuf()->in_avail() > 0`, extracts `std::min(rdbuf()->in_avail(), count)` characters and stores them into successive locations of the character array whose first element is pointed to by `s`.

### Parameters

- `s` - pointer to the character array to store the characters to
- `count` - maximum number of characters to read

### Return value

The number of characters actually extracted.

### Exceptions

`failure`

if an error occurred (the error state flag is not

`goodbit`

) and

`exceptions()`

is set to throw for that state.

If an internal operation throws an exception, it is caught and `badbit` is set. If `exceptions()` is set for `badbit`, the exception is rethrown.

### Notes

The behavior of this function is highly implementation-specific. For example, using `readsome()` with `std::ifstream` leads to significant, implementation-specific outcomes. Some library implementations fill the underlying `filebuf` with data as soon as `std::ifstream` opens a file, which means `readsome()` always reads data and could even read the entire file. With other implementations, `std::ifstream` only reads from a file when an input operation is invoked, which means calling `readsome()` immediately after opening the file never extracts any characters. Similarly, calling `std::cin.readsome()` may return all pending, unprocessed console input or may always return zero and extract no characters.

### Example

```cpp
#include <cassert>
#include <iostream>
#include <sstream>

int main()
{
    char c[10] = "*********"; // c[9] == '\0'

    // std::stringbuf makes its entire buffer available for unblocking read
    std::istringstream input("This is sample text.");

    auto r = input.readsome(c, 5); // reads 'This ' and stores in c[0] .. c[4]
    assert(r == 5);
    std::cout << c << '\n';

    r = input.readsome(c, 9); // reads 'is sample' and stores in c[0] .. c[8]
    assert(r == 9);
    std::cout << c << '\n';
}
```

Output:

```text
This ****
is sample
```

### See also

- [`read`](https://en.cppreference.com/w/cpp/io/basic_istream/read): extracts blocks of characters (public member function)
- [`in_avail`](https://en.cppreference.com/w/cpp/io/basic_streambuf/in_avail): obtains the number of characters immediately available in the get area (public member function of `std::basic_streambuf<CharT,Traits>`)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/basic_istream/readsome (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.