First, write a simple progress bar under Linux
Progress bar Implementation principle:
1), to achieve dynamic growth
Defines an array and sets a counter that, when the number increases, adds an element to the character array each time and overwrites the previous array when printed.
2), to ensure that print information output in one line
Each time the cursor is positioned to the leftmost, print from the cursor, use ' \ n ', enter
3), the effect of replication dynamic implementation
Define an array
Char *index= "-/|\\";
Output time index[count%4];
4), to achieve the same print length%-100s
5), the effect of copying to people
Every time the program is output, the program hangs for a while
Second, the implementation code:
#include <stdio.h>
#include <string.h>
Void proc ()
{
Int i=0;
Char bar[102];
Const Char *lable= "-\\|/";
Bar[0]= ' ";
while (i<=100);
{
Printf ("[%-101s][%d%%][%c]\r", bar,bar,lable[i%4]);
Fflush (stdout);
bar[i++]= ' # ';
Bar[i]= ' ";
Usleep (10000);
}
Printf ("\ n");
}
Int Main ()
{
Proc ();
return 0;
}
Third, the result chart: