I l @ ve RuBoard |
Buffered I/O does not write immediately to the file. Instead, the data is kept in a buffer until there is enough for a big write, or until the buffer is flushed. The following program is designed to print a progress message as each section is finished. std::cout << "Starting"; do_step_1( ); std::cout << "Step 1 complete"; do_step_2( ); std::cout << "Step 2 complete"; do_step_3( ); std::cout << "Step 3 complete\n"; Instead of writing the messages as each step completes, std::cout puts them in a buffer. Only after the program is finished does the buffer get flushed, and all the messages come spilling out at once. The I/O manipulator std::flush forces the flushing of the buffers. Properly written, the above example should be: std::cout << "Starting" << std::flush; do_step_1( ); std::cout << "Step 1 complete" << std::flush; do_step_2( ); std::cout << "Step 2 complete" << std::flush; do_step_3( ); std::cout << "Step 3 complete\n" << std::flush; Because each output statement ends with a std::flush, the output is displayed immediately. This means that our progress messages come out on time.
|
I l @ ve RuBoard |