On the implementation of the progress bar, the main point is dynamic modification, and is in-line buffer update.
In this respect, you need to understand the row buffer problem with printf ().
Can do the following verification
#include <stdio.h><stdlib.h>int main () { // printf ("test") ; Case1:3 seconds after the output printf ("test\n"); // case2: Output now Sleep (3); return 0 ;}
The difference between the two case is \ n, so the crux of the problem is \ n.
printf is a row buffer function that is written to the buffer before it satisfies the condition before the buffer is brushed into the corresponding file, and the condition of the brush buffer is as follows:
1 buffer fills up.
2 The characters written are ' \ n ', ' \ R '.
3 Call Fflush to flush the buffer manually.
4 Call scanf the data in the buffer is also flushed when the data is being read from the buffer.
One of the 4 conditions above satisfies that the buffer will refresh, that is, printf will actually call write to write.
When we execute the printf process or the thread ends, it will call flush to flush the buffer, so the program ends and refreshes.
For the above points, it is easy to verify, here is not separate talk about
About the progress bar.
With the understanding of the buffer problem, we can grasp the timing and way of the main update buffer.
When the progress bar advances, the update buffer is re-written, and the method is \ r. Because our progress bar is a line to
Code implementation
#include <stdio.h>#include<string.h>voidproc () {intRate =0; Charbar[102]; memset (Bar,' /',sizeof(bar)); Const Char* Flag ="|/-\\"; while(Rate <= -) {Bar[rate]='='; printf ("[%-101s][%%%d][%c]\r", bar,rate,flag[rate%4]); Fflush (stdout); Rate++; Sleep (1); }}intMain () {proc (); return 0;}
The problem with this progress bar is just a simple row buffer understanding and a short project experience. The progress bar in the actual project is not so.
Personal understanding, to think:: Because the actual program in the progress bar is required to meet the tool, the specific function of the progress achieved. So the implementation of the progress bar is not a valid action on a timeline.
For example, file compression, for a few points in the process of the relative control, and then a progress bar as a companion process, the use of inter-process relations (or observer mode), the program is a step forward, report to the progress bar program to advance a step.
To achieve real progress to grasp.
Again such as network file download, first: You can determine the exact size of the read file, at this time, is based on the network transfer rate, file transfer ratio to update follow-up progress bar program.
The progress bar in the project realizes the imaginary