cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ostream : flush
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
ostream
· ostream::ostream
· ostream::~ostream
member classes:
· ostream::sentry
member functions:
· ostream::flush
· ostream::operator<<
· ostream::put
· ostream::seekp
· ostream::tellp
· ostream::write

-

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 manipultor 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

// 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

flush Flush stream buffer (manipulator function)
endl Insert newline and flush (manipulator function)
istream::sync Synchronize input buffer with source of characters (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us