ostream::flush


public member function
ostream& flush ( );

Flush output stream buffer

Synchronizes the buffer associated with the stream to its controlled output sequence. This effectively means that all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").

The function only has meaning for buffered streams, in which case it effectively calls the pubsync member of the streambuf object (rdbuf()->pubsync()) associated to the stream.

A manipulator exists with the same name and behavior (see flush manipulator).

Parameters

none

Return Value

The function returns *this.

If an error happens while flushing a buffered stream, the badbit flag is set (which can be checked with member bad). Also, depending on the values set through member exceptions, this may cause an exception of type ios_base::failure to be thrown.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Flushing files
#include <fstream>
using namespace std;

int main () {

  ofstream outfile ("test.txt");

  for (int n=0; n<100; n++)
  {
    outfile << n;
    outfile.flush();
  }

  outfile.close();

  return 0;
}


When this example is executed the content of the file test.txt is updated 100 times.

Basic template member declaration

( basic_ostream<charT,traits> )
 
basic_ostream& flush ( );


See also