What is a progress bar? The progress bar is when you download files, software, games and other progress, it will show you how much you can download this thing.
Because of my curiosity, I realized this thing under Linux.
My idea of realization:
1. To an array, this array must be able to hold the next 100 characters.
2. Then read the array as a line.
3. The final standard output is on the screen.
Attention:
1. We want to print a progress bar is definitely 100%, so it is necessary to be able to just under 100 characters of the array, but need to be aware of is to open up a word descriptors '/0 ', open up a good later also need to initialize all to '/0 ' to prevent garbled characters when the output string.
2. The most important thing is to add printf ("/R"), this is used to only enter the non-newline, can achieve the function is to buffer the value of the inside is always only one row, does not appear each time to output a string of characters, so that the core function of the progress bar can be achieved.
3. When printing the progress bar, you need to pin both sides of the box, so write%100s but need to add a minus sign, because it is printed from the right to start
Implementation code:
#include <stdio.h> #include <string.h>void proc () {int rate = 0;const char *lable = "|/-\\"; char bar[102]; memset (bar, ' + ', sizeof (bar));//Initialize while (rate <=) {Bar[rate] = ' = ';p rintf ("[ -101s][%d%%][%c]\r", bar, rate, Lable[rate% 4]); Fflush (stdout);//Standard output rate++;usleep (10000);} printf ("\ n");} int main () {proc (); return 0;}
Output Result:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/63/wKioL1dT5HehpO6GAAAMmKHYGsE340.png-wh_500x0-wm_3 -wmp_4-s_2828432953.png "title=" Qq20160605163224.png "alt=" Wkiol1dt5hehpo6gaaammkhygse340.png-wh_50 "/>
Linux Simulation Implementation progress bar