Flush in C++
The word "flush" in C++ refers to causing content inside an output stream, like std::cout, to be sent to the fundamental device immediately instead of waiting for the input stream to clear its buffer automatically. Data is frequently kept in an internal buffer before being published to the real output when written to an input stream, such as publishing to the console with std::cout. Pushing data from a buffer onto the output device is called flushing the buffer.
To request immediate cleaning of the buffer connected to an output stream, use the std::flush Manipulator. It has two applications:
- Std::flush manipulator usage:
Syntax:
std::cout << "This will be flushed immediately." << std::flush;
Program:
#include <iostream>
#include <chrono>
#include <thread>
int main() {
std::cout << "Output without flush." << std::endl;
std::cout << "Flushing output..." << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(2)); // Introducing a delay
std::cout << "Output after flush." << std::endl;
return 0;
}
Output:
![Flush in C++](https://static.tutorialandexample.com/cpp/flush-in-cpp1.png)
Explanation:
In this program, the sentence "Flushing output..." is followed by the std::flush Manipulator. As a result, the line "Flushing output..." will appear before the delay caused by std::this_thread::sleep_for. It requires the buffer to be flushed promptly. The delay represents a brief processing period before printing the "Output when flush." line.
- std::endl manipulator usage:
Syntax:
std::cout << "This will also be flushed immediately." << std::endl;
Program:
#include <iostream>
#include <chrono>
#include <thread>
int main() {
std::cout << "Output without flush." << std::endl;
std::cout << "Flushing output..." << std::endl; // Using endl to flush
std::this_thread::sleep_for(std::chrono::seconds(2)); // Introducing a delay
std::cout << "Output after flush." << std::endl;
return 0;
}
Output:
![Flush in C++](https://static.tutorialandexample.com/cpp/flush-in-cpp2.png)
Explanation:
In this program, the sentence "Flushing output..." is followed by the std::endl Manipulator. It ensures that the content is displayed before the delay produced by std::this_thread::sleep_for and adds a new line character ('n') to the stream. The delay represents a brief processing period before printing the "Output after flush." line.
Parameters of using flash:
The std::flush Manipulator in C++ does not need any parameters. It is used to specifically ask for the quick flushing of the buffer connected to the stream it is applied to. The syntax to utilize std::flush is as follows:
std::cout << "Some text" << std::flush;
You wish to print "Some text" in this case, and std::flush is utilized to clear the output buffer once the text has been written.
Alternatively, you may use the std::endl manipulator to get the same flushing result:
std::cout << "Some text" << std::endl;
Std::endl adds a newline character ('n') to the input stream before flushing the buffer, whereas std::flush only achieves the identical flushing behavior. Due to this, std::endl is occasionally preferable when you expressly wish to insert a new line.
Different ways of using flush:
- The std::flush Manipulator is used:
To explicitly flush the result buffer, use the std::flush Manipulator. It is applied immediately to the resultant stream and requires no arguments.
Syntax:
std::cout << "Hello, " << "world!" << std::flush;
- Applying the Manipulator std::endl:
The std::endl Manipulator flushes the output stream and inserts a newline character. Functionally speaking, it's the same as using "\n" and then std::flush:
Syntax:
std::cout << "Hello, " << "world!" << std::endl;
- Utilizing the member function flush():
A member method named flush() is available for output streams, and it may be employed to flush that stream explicitly:
Syntax:
std::cout << "Hello, ";
std::cout.flush();
std::cout << "world!";
- Automatic Flushing:
Output streams are configured by default to flush immediately upon encountering a newline character (this behavior is also included in std::endl). To deactivate synchronization between C and C++ streams, modify this behavior using the std::ios_base::sync_with_stdio(false) method. But be aware that combining stream operations from C and C++ might result in strange behavior.
- Error stream flushing:
Flushing can be necessary for error streams such as std::cerr to guarantee that crucial error messages are displayed to the user right away:
Syntax:
std::cerr << "An error occurred." << std::flush;
- Flushing Manipulator Output Streams:
Define your very own manipulators that carry out a flush operation as well. When you wish to encapsulate specific formatting behaviors in addition to flushing, this might be helpful:
Syntax:
std::ostream& flush_and_print(std::ostream& os) {
os << std::flush;
return os;
}
// Usage:
std::cout << "Flushing custom manipulator: " << flush_and_print << "done!";
Key points:
Mechanisms of Flushing:
- Data that has been buffered in the stream must be emitted immediately by the std::flush Manipulator.
- The manipulator std::endl flushes the stream and adds a new line character.
- To explicitly flush the stream, use the flush() member of the output streams.
Flushing Automatically:
- Most output streams automatically flush by default when they hit the newline character ('\n'). This behavior guarantees that lines appear in the output right away.
Performance-Related Aspects:
- Overuse of flushing might affect performance since it incurs costs when publishing data to the resultant device.
- Buffers are made to reduce the amount of write operations required, thereby increasing efficiency.
Accurate Timekeeping Using C Streams:
- In some situations, speed can be increased by turning off synchronization between the C and C++ streams using the std::ios_base::sync_with_stdio(false) method.
- However, mixing stream operations in C and C++ might result in strange behavior.
Mistake Streams:
- Flushing is especially crucial for failure streams like std::cerr since it guarantees that users will immediately see essential error messages.
Personalized Manipulators:
- You can create personalized manipulators that carry out formatting behaviors in addition to a flush operation.