public member function
<ostream> <iostream>
Flush output stream buffer
Synchronizes the associated stream buffer with its controlled output sequence.
For stream buffer objects that implement intermediate buffers, this function requests all characters to be written to the controlled sequence.
Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it calls pubsync on its associated stream buffer object, and finally destroys the sentry object before returning.
A manipulator exists with the same name and behavior (see flush).
Return Value
The basic_ostream object (*this
).
Errors are signaled by modifying the internal state flags:
flag | error |
eofbit | - |
failbit | May be set if the construction of sentry failed. |
badbit | The synchronization operation failed (including if the function catches an exception thrown by an internal operation).
When set, the integrity of the stream may have been affected.
|
Multiple flags may be set by a single operation.
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// Flushing files
#include <fstream> // std::ofstream
int main () {
std::ofstream outfile ("test.txt");
for (int n=0; n<100; ++n)
{
outfile << n;
outfile.flush();
}
outfile.close();
return 0;
}
|
This example attempts to update the content of file test.txt
100 times.
Data races
Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog, wcout, wcerr and wclog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
Exception safety
Basic guarantee: if an exception is thrown, the object is in a valid state.
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.
See also
- flush
- Flush stream buffer (function
)
- endl
- Insert newline and flush (function
)
- basic_istream::sync
- Synchronize input buffer (public member function
)