C++ nounitbuf Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, nounitbuf turns off unitbuf. The stream goes back to normal buffering: it collects output and sends it in bigger pieces, which is faster.

This is the default state of cout and file streams. It is useful when you changed a stream to unit buffering, or when you want to speed up cerr. 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::nounitbuf into an output stream.

C++
std::cerr << std::nounitbuf;     // cerr now buffers
out << std::unitbuf << "A\n" << std::nounitbuf << "B\n";
CallWhat it does
std::cout << std::nounitbufNormal buffering for cout.
std::cerr << std::nounitbufStops the automatic flushing of cerr.
out << std::unitbuf << a << std::nounitbuf << bFlushes after a but not after b.

Turn buffering back on only for the output that must be seen at once.

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

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << nounitbuf. 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 nounitbuf.

C++ Examples

Example 1 — Turn Off Unit Buffering of cerr

Check the flag on cerr before and after.

C++
#include <iostream>

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

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

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

How It Works

  1. cerr starts with unitbuf on, so the first line says yes.
  2. std::cerr << std::nounitbuf switches it off. Nothing is printed by this statement.
  3. The flag is now off, so the second line says no. Warnings now wait in the buffer.

Example 2 — See the Difference in a File

Flush the first line only. A second stream sees just that line until the file is closed. The program creates nounitbuf_demo.txt.

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

int count_lines(const char* name) {
    std::ifstream in(name);
    std::string line;
    int n = 0;
    while (std::getline(in, line)) {
        n++;
    }
    return n;
}

int main() {
    std::ofstream out("nounitbuf_demo.txt");
    out << std::unitbuf << "A\n" << std::nounitbuf << "B\n";

    std::cout << "lines visible now: " << count_lines("nounitbuf_demo.txt") << '\n';
    out.close();
    std::cout << "lines after close: " << count_lines("nounitbuf_demo.txt") << '\n';
    return 0;
}

How It Works

  1. unitbuf is on while A is written, so it is flushed to the file at once.
  2. nounitbuf comes next, so B stays in the stream buffer. A second stream can read only one line.
  3. out.close() flushes what is left, and then both lines are in the file.

Example 3 — Switch Buffering On and Off Again

Turn the flag on, check it, and turn it off, so the stream is as it was.

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

int main() {
    std::ostringstream out;

    out << std::unitbuf;
    bool on = (out.flags() & std::ios::unitbuf) != 0;

    out << std::nounitbuf;
    bool off = (out.flags() & std::ios::unitbuf) != 0;

    std::cout << "after unitbuf:   " << (on ? "on" : "off") << '\n';
    std::cout << "after nounitbuf: " << (off ? "on" : "off") << '\n';
    return 0;
}

How It Works

  1. A std::ostringstream accepts the same manipulators as cout.
  2. The flag is checked after unitbuf and again after nounitbuf, and the answers are stored in two bool variables.
  3. The results show on and then off. The stream is back in its normal state.

Frequently Asked Questions

It turns off unit buffering, so the stream stops flushing after every output and collects text in its buffer again.
For cout and file streams, yes. For cerr, no: cerr has unitbuf on by default.
To make a program that prints many warnings faster. The price is that messages may appear later.
When the buffer is full, when you call flush() or endl, when the stream is closed, or when the program ends normally.
cout.unsetf(std::ios::unitbuf) has the same effect.

Next: nouppercase

Go back to small letters in hex digits and exponents.

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

13 people found this page helpful