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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, read(buffer, count) copies count characters from an input stream into buffer. It reads spaces, new lines, and even zero bytes without stopping, and it adds no \0 at the end.

It is the matching function of write() and is mostly used for binary files and for reading data in fixed chunks. 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 read on an input stream. Give the buffer and the number of characters you want.

C++
char buf[16];
in.read(buf, 16);
std::streamsize got = in.gcount();    // how many arrived
CallWhat it does
in.read(buf, 5)Reads five characters into buf.
in.read(reinterpret_cast<char*>(&n), sizeof n)Reads the raw bytes of an int written by write().
while (in.read(buf, 4))Reads full chunks of four. The loop stops when a full chunk cannot be read.

If fewer characters are available, read() stores what it can and sets eofbit and failbit. Ask gcount() how many arrived.

Library declarations
C++
istream& read(char_type* s, streamsize count);

Parameters

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

Return Value

Return ValueDescription
istream&A reference to the stream. It is false in a condition when fewer than count characters could be read.

C++ Examples

Example 1 — Read Five Characters

Read a block of text and add the final zero yourself.

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

int main() {
    std::istringstream in("HelloWorld");
    char buf[6];

    in.read(buf, 5);
    buf[5] = '\0';

    std::cout << "block: " << buf << '\n';
    std::cout << "next: " << static_cast<char>(in.get()) << '\n';
    return 0;
}

How It Works

  1. buf has six places: five for the letters and one for the end marker.
  2. read(buf, 5) copies Hello into it. Nothing adds the zero, so the program sets buf[5] itself.
  3. The stream continues after the block, so the next get() returns W.

Example 2 — Write and Read Back an int

Store the bytes of a number in a string stream and read them into another variable.

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

int main() {
    int original = 1234;
    int copy = 0;

    std::stringstream ss;
    ss.write(reinterpret_cast<const char*>(&original), sizeof original);
    ss.read(reinterpret_cast<char*>(&copy), sizeof copy);

    std::cout << "copy = " << copy << '\n';
    return 0;
}

How It Works

  1. ss.write() puts the four raw bytes of original into the stream.
  2. ss.read() takes the same number of bytes out and stores them directly in copy.
  3. The bytes were never turned into text, so the value arrives unchanged: 1234.

Example 3 — Read in Chunks of Four

Loop while a full chunk can be read, then handle the shorter last piece with gcount().

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

int main() {
    std::istringstream in("abcdefghij");
    char buf[5];

    while (in.read(buf, 4)) {
        buf[4] = '\0';
        std::cout << "chunk: " << buf << '\n';
    }

    std::streamsize n = in.gcount();
    buf[n] = '\0';
    std::cout << "last chunk: " << buf << " (" << n << " chars)\n";
    return 0;
}

How It Works

  1. The loop condition in.read(buf, 4) is true only when all four characters were read.
  2. abcd and efgh are full chunks and are printed. Only ij remains, so the third read gets two characters and fails.
  3. The data is still in buf. gcount() says 2, so the program ends the string at that place and prints ij.

Frequently Asked Questions

It reads exactly count characters (bytes) from an input stream into a buffer without skipping spaces or new lines.
>> reads a formatted value and skips spaces. read() copies raw characters, so it can read spaces, new lines, and binary data.
No. You must add the \0 yourself if you want to treat the buffer as a C string.
read() stores what it can and sets eofbit and failbit. Call gcount() to see how many bytes arrived.
Open an ifstream with std::ios::binary and call read(reinterpret_cast<char*>(&value), sizeof value), or read into a char buffer.

Next: readsome()

Read only the characters that are ready right now.

readsome() 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.

15 people found this page helpful