Home C++ Iostream Functions write() C++ std::ostream::write() Function 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.
std::cout.write("Hello, World", 5); // writes Hello
out.write(text.data(), text.size());
out.write(reinterpret_cast<const char*>(&n), sizeof n); Call What 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 ostream& write(const char_type* s, streamsize count); Parameters Parameter Description 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 Value Description ostream&A reference to the stream. If writing failed, badbit is set.
Important
write() trusts you about count. If you give a count that is larger than the real data, it reads beyond the end, which is undefined behavior. For files with binary data, open the stream with std::ios::binary.
C++ Examples 1. Part of a text 2. A std::string 3. Binary data Example 1 — Write Only Part of a String The count decides how many characters appear, even if the text is longer.
#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 write(text, 5) sends the first five characters, Hello.text + 7 points to the W after the comma and the space. Five characters from there are World.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.
#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 msg.data() gives a pointer to the characters, and msg.size() gives their count, 6.The second write() adds the three characters -ok. 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.
#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;
} bytes written: 4
same as sizeof(int): yes How It Works &n is the address of the number. reinterpret_cast<const char*> lets write() treat that memory as characters.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.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 What does ostream::write() do? It writes exactly count characters from a buffer to an output stream, with no formatting and no stopping at a zero character.
What is the difference between write() and <<? << 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.
How do I write binary data to a file? Open an ofstream with std::ios::binary and call write(reinterpret_cast<const char*>(&value), sizeof value).
Does write() add a new line or a zero at the end? No. It writes only the characters you asked for.
What is the matching input function? read(). It reads a fixed number of raw characters from an input stream into a buffer.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion