function
<ios> <iostream>

std::unitbuf

ios_base& unitbuf (ios_base& str);
Flush buffer after insertions
Sets the unitbuf "format" flag for the str stream.

When the unitbuf flag is set, the associated buffer is flushed after each insertion operation.

This flag can be unset with the nounitbuf manipulator, not forcing flushes after every insertion.

For standard streams, the unitbuf flag is not set on initialization.

Parameters

str
Stream object whose format flag is affected.
Because this function is a manipulator, it is designed to be used alone with no arguments in conjunction with the insertion (<<) and extraction (>>) operations on streams (see example below).

Return Value

Argument str.

Example

1
2
3
4
5
6
7
8
9
10
// modify unifbuf flag
#include <ios>         // std::unitbuf
#include <fstream>     // std::ofstream

int main () {
  std::ofstream outfile ("test.txt");
  outfile << std::unitbuf <<  "Test " << "file" << '\n';  // flushed three times
  outfile.close();
  return 0;
}


Data races

Modifies str. Concurrent access to the same stream object may cause data races.

Exception safety

Basic guarantee: if an exception is thrown, str is in a valid state.

See also