C++ unitbuf Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, unitbuf turns on unit buffering: after every output operation the stream flushes its buffer at once. Text then reaches the screen or file without waiting.

The standard error stream std::cerr already works this way, so error messages show up immediately. You can switch the same behaviour on for cout or a file stream. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::unitbuf into an output stream.

C++
std::cout << std::unitbuf;       // flush after every <<
out << std::unitbuf << "text\n";
CallWhat it does
std::cout << std::unitbufTurns on flush after every output.
std::cout.flags() & std::ios::unitbufNon-zero when the flag is on.
std::cout << std::nounitbufTurns the behaviour off again.

Flushing after every output is slow for large amounts of text. Use unitbuf for logs and progress messages, not for bulk output.

Library declarations
C++
ios_base& unitbuf(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << unitbuf. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after unitbuf.

C++ Examples

Example 1 — See Which Streams Use unitbuf

Ask each stream whether the flag is on. cerr has it from the start.

C++
#include <iostream>

bool unit(const std::ostream& s) {
    return (s.flags() & std::ios::unitbuf) != 0;
}

int main() {
    std::cout << "cout before: " << (unit(std::cout) ? "yes" : "no") << '\n';

    std::cout << std::unitbuf;
    std::cout << "cout after:  " << (unit(std::cout) ? "yes" : "no") << '\n';
    std::cout << "cerr:        " << (unit(std::cerr) ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. flags() returns all format flags. Testing with & std::ios::unitbuf tells whether this one is on.
  2. cout starts without the flag, and std::unitbuf turns it on.
  3. cerr is made with unitbuf already on. This is why error messages appear at once, even if the program crashes.

Example 2 — Show Progress Without Calling flush

With unitbuf each dot is sent as soon as it is written.

C++
#include <iostream>

int main() {
    std::cout << std::unitbuf;

    std::cout << "Working";
    for (int i = 0; i < 3; i++) {
        std::cout << '.';
    }
    std::cout << " done\n";
    return 0;
}

How It Works

  1. std::unitbuf is put in once. From now on, every output flushes.
  2. Each '.' reaches the screen right away, so a user would see the dots appear one by one.
  3. Without unitbuf you would need << std::flush after each dot to get the same effect.

Example 3 — unitbuf on a File Stream

The data can be read by another stream at once, even before the file is closed. The program creates unitbuf_demo.txt.

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

int main() {
    std::ofstream out("unitbuf_demo.txt");
    out << std::unitbuf;
    out << "saved\n";

    std::ifstream in("unitbuf_demo.txt");
    std::string line;
    std::getline(in, line);

    std::cout << "read back: " << line << '\n';
    return 0;
}

How It Works

  1. out << std::unitbuf makes the file stream flush after every write.
  2. out << "saved\n" writes the line and flushes it to the file at once.
  3. The second stream finds the line, although out is still open and was never closed or flushed by hand.

Frequently Asked Questions

It makes a stream flush its buffer after every output operation, so text is sent to its destination immediately.
Yes. cerr has unitbuf set by default, so error messages appear at once.
Not exactly. endl writes a new line and flushes once. unitbuf flushes after every single output, which is the same as writing flush after each <<.
Yes, if you write a lot of small pieces, because every piece is a separate flush. Use it for logs and progress, not for large output.
Use std::nounitbuf, or cout.unsetf(std::ios::unitbuf).

Next: uppercase

Use capital letters in hex digits and exponents.

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

10 people found this page helpful