C++ std::ostream::write() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, write(buffer, count) sends count characters from buffer to an output stream. It does not stop at a \0 and it does not format anything.

It is the right tool when you know exactly how many characters to write, and it is used for binary data in files. Include <iostream> to use it.

This is a member function of std::ostream. 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 write on an output stream. Give a pointer to the characters and how many to write.

C++
std::cout.write("Hello, World", 5);       // writes Hello
out.write(text.data(), text.size());
out.write(reinterpret_cast<const char*>(&n), sizeof n);
CallWhat it does
std::cout.write("Hello", 3)Writes only the first three characters, Hel.
out.write(s.data(), s.size())Writes the whole content of a std::string.
out.write(reinterpret_cast<const char*>(&n), sizeof n)Writes the raw bytes of a variable. This is binary output.

For normal text, << is simpler. Use write() for exact counts and for binary data.

Library declarations
C++
ostream& write(const char_type* s, streamsize count);

Parameters

ParameterDescription
sRequired. A pointer to the first character to write.
countRequired. How many characters to write. It must not be larger than the data that s points to.

Return Value

Return ValueDescription
ostream&A reference to the stream. If writing failed, badbit is set.

C++ Examples

Example 1 — Write Only Part of a String

The count decides how many characters appear, even if the text is longer.

C++
#include <iostream>

int main() {
    const char* text = "Hello, World";

    std::cout.write(text, 5);
    std::cout << '\n';
    std::cout.write(text + 7, 5);
    std::cout << '\n';
    return 0;
}

How It Works

  1. write(text, 5) sends the first five characters, Hello.
  2. text + 7 points to the W after the comma and the space. Five characters from there are World.
  3. write() adds no new line, so the program prints one with <<.

Example 2 — Write a std::string to a String Stream

Use data() and size() to write the whole content.

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

int main() {
    std::string msg = "stream";
    std::ostringstream out;

    out.write(msg.data(), msg.size());
    out.write("-ok", 3);

    std::cout << out.str() << '\n';
    std::cout << "length: " << out.str().size() << '\n';
    return 0;
}

How It Works

  1. msg.data() gives a pointer to the characters, and msg.size() gives their count, 6.
  2. The second write() adds the three characters -ok.
  3. out.str() returns the collected text. 6 + 3 characters make a length of 9.

Example 3 — Write the Raw Bytes of a Number

Write an int as bytes, not as text. The size is the size of the type.

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

int main() {
    int n = 1234;
    std::ostringstream out;

    out.write(reinterpret_cast<const char*>(&n), sizeof n);

    std::cout << "bytes written: " << out.str().size() << '\n';
    std::cout << "same as sizeof(int): "
              << (out.str().size() == sizeof(int) ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. &n is the address of the number. reinterpret_cast<const char*> lets write() treat that memory as characters.
  2. sizeof n is the number of bytes in an int, so exactly that many bytes are written. On most computers an int has 4 bytes.
  3. The text 1234 would be four characters, but the binary form is the raw value. Its length depends on the type, not on the digits.

Frequently Asked Questions

It writes exactly count characters from a buffer to an output stream, with no formatting and no stopping at a zero character.
<< formats values and stops text at the end of a C string. write() sends a fixed number of raw characters and does not change them.
Open an ofstream with std::ios::binary and call write(reinterpret_cast<const char*>(&value), sizeof value).
No. It writes only the characters you asked for.
read(). It reads a fixed number of raw characters from an input stream into a buffer.

Next: boolalpha

Print and read bool values as true and false.

boolalpha 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.

9 people found this page helpful