The topic of this article is to implement a progress bar, the application of the progress bar in the software everywhere, copy a file requires a progress bar, loading a file also need a progress bar, to mark the completion or not.
So, what are the elements of a progress bar:
A container that keeps growing to the right (intuitively seeing the current progress)
A percentage of the data that reflects progress.
A sign (this flag indicates whether the progress bar is working or is stuck)
Here are some small points of knowledge: the buffer in the C language is refreshed in a row buffer. That is, the program takes a line-ending flag (which can be a newline character and EOF) in the input stream to be displayed on the output device (screen). Note that the system forces the buffer to flush when the program ends.
As needed, we can use a character array to act as a container for the progress bar. Initially initialized to 0, each loop is modified to ' = ', and then the current array is printed, because 0 is the Ascall code of the newline character ' \ n ', so there is no time to print only to the modified place. Then in the carriage return, but do not wrap (' \ R ') on sleep for some time. We can see the dynamic growth of the container.
Here's how it's implemented:
1 #include <stdio.h> 2 #include <string.h> 3 int main () 4 { 5 char str[101]={0}; 6 char arr[4]={' | ', '/', '-', ' \ \ '}; 7 int i=0; 8 for (; i<101;++i) 9 { 10 str[i]= ' * '; 11 printf ("[%-100s][%c][%d\%]", str,arr[i%4],i%101); 12 printf ("\ r"); 13 &Nbsp; usleep (100000); 14 fflush (STDOUT) ; 15 } 16 printf ("\ n"); 17 return 0; 18 }
Run:
650) this.width=650; "Src=" http://img.blog.csdn.net/20160531200154648?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "/>
This article is from the "Straw Sunshine" blog, please be sure to keep this source http://helloleex.blog.51cto.com/10728491/1785017
Progress bar applet under Linux