introduce
std::flush is an operator in the C++ standard library that refreshes the output stream. Refreshing the output stream means sending data in the buffer immediately to the associated output device (such as a screen or file). In some cases, the output stream is automatically flushed, for example when the stream buffer is full, but using std::flush can force the buffer to be refreshed immediately.
Use scenarios
- debug: During development, when you need to see the output result of a variable or expression immediately, you can use std::flush. This helps keep track of the running status of the program, especially when debugging complex issues.
- Real-time progress instructions: When the program runs for a period of time before completing tasks (such as file downloading, data processing, etc.), you can use std::flush to provide real-time progress indication. This way, users can see the progress of the program instead of feeling confused while waiting for the result.
- Ensure log integrity: When writing information to a log file, it may be desirable to write the contents of the buffer to the file before an exception or crash of the program. Using std::flush ensures that the log file reflects the latest status of the program.
Example
Debugging scenarios
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Long operation in progress: "; for (int i = 0; i < 5; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << i + 1 << ", " << std::flush; } std::cout << "Operation completed!" << std::endl; return 0; }
Print result:
Done!Long operation in progress: 1, 2, 3, 4, 5, Operation completed!
In the actual console, you will see that a number is output every second, which is displayed one by one, rather than one after the operation is completed. This is because we use std::flush on each iteration to ensure the buffer is flushed immediately.
If you do not use std::flush, you may see all outputs being cached on some systems and compilers until the operation is displayed at once. However, actual results may vary depending on the operating system, compiler, and program running environment. In some cases, the output may be displayed immediately even without using std::flush. But using std::flush ensures that the output is displayed immediately in all cases.
Real-time progress indication scenario
#include <iostream> #include <chrono> #include <thread> int main() { const int total_steps = 10; for (int i = 0; i <= total_steps; ++i) { std::cout << "\rProgress: " << (i * 100 / total_steps) << "%" << std::flush; std::this_thread::sleep_for(std::chrono::milliseconds(500)); } std::cout << std::endl; return 0; }
Print result:
Progress: 0%->100%
In this example, we use std::flush to display the progress percentage in real time. When the program is executed, the progress percentage is updated on the same line, providing real-time feedback.
Ensure log integrity scenario
#include <iostream> #include <fstream> #include <chrono> #include <thread> void log_message(const std::string& message, std::ofstream& log_file) { log_file << message << std::flush; } int main() { std::ofstream log_file(""); if (!log_file) { std::cerr << "Unable to open log file." << std::endl; return 1; } for (int i = 0; i < 5; ++i) { std::string message = "Processing step " + std::to_string(i) + "..."; log_message(message, log_file); std::this_thread::sleep_for(std::chrono::seconds(1)); log_message(" Done!\n", log_file); } log_file.close(); return 0; }
Log file content:
Processing step 0… Done!
Processing step 1… Done!
Processing step 2… Done!
Processing step 3… Done!
Processing step 4… Done!
In this example, we create a simple logging function that writes information to a log file. We use std::flush in the logging function to ensure that every step in the program execution is immediately written to the log file. This helps ensure the integrity of the log file even if the program terminates unexpectedly.
Note: The actual situation may vary depending on the operating system, compiler and program running environment. In some cases, the output may be written to the log file immediately even without using std::flush.However, on some systems, the output buffer may not be refreshed immediately, resulting in the log file not reflecting the actual status of the program in time.The purpose of using std::flush is to ensure that the contents of the buffer can be written to the log file immediately in all cases. This way, even if the program is abnormal or crashed, you can ensure that the log file reflects the latest status during the program execution. Of course, in many cases, it will work properly with or without std::flush.But using std::flush in critical parts can lead to higher log reliability, especially when dealing with large amounts of data, cross-platform development, or handling critical tasks.
This is the article about the specific use of the C++ library std::flush. For more related C++ std::flush content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!