First create a new file, Touch progress_bar.c execute the vim progress_bar.c command, write the progress bar of the program. Write into a progress bar program:
#include <stdio.h>#include <unistd.h>#include <string.h>voidProgress () {inti =0;Charbar[102];memset(Bar,0,102*sizeof(Char));Const Char* lable="|/-\\"; while(I <= -) {Bar[i] =' # ';printf("[%-101s] [%d%%] [%c]", bar,i,lable[i%4]); Fflush (stdout); Usleep100000); i++; }printf("\ n");}intMain () {progress ();return 0;}
Small details to note in this code:
1. const char* lable= "|/-\\"; The direct input of a \ will be considered escaped by the system, so enter \ \
2. printf ("[%-101s] [%d%%] [%c]", bar,i,lable[i%4]), here the percent of the same, to prevent escaping. rate%4 Prevent overflow
3. fflush (stdout); parameter is standard output stream
4. because sleep default unit is seconds, not easy to test, usleep default unit is microseconds
Finally, debug, create a mymakefile file, touch mymakefile to edit the file vim Mymakefile.
myprogress_bar:progress_bar.c g++ -o myprogress_bar progress_bar.c:PHONY clean clean: rm -f myprogress_bar
:
then execute the make command, compile the progress_bar.c file, Make-f mymakefile, generate the Myprogress_bar file and execute it with./myprogress_bar. If you want to recompile, you will need to make-f the mymakefile Clean directive to clear the file Progress_bar before compiling with make.
Write a simple progress bar under Linux and compile with the make command.