(1)创建一个Makefile文件:touch Makefile
(2)打开Makefile:vim Makefile
(3)将以下内容写入:
pro_bar:progress_bar.c
gcc progress_bar.c -o pro_bar
.PHONY:clean
clean:
rm -f pro_bar
(4)创建一个progress_bar.c的文件:touch progress_bar.c
(5)打开progress_bar.c文件:vim progress_bar.c
(6)将以下代码写入:
#include <unistd.h>
#include <stdio.h>
#include <string.h>#define NONE "\e[0m"
#define RED "\e[0;31m"
#define BLUE "\e[0;34m"
int main()
{
int i=0;
char bar[102];
memset(bar,0,sizeof(bar));
const char *lable="|/-\\";
while(i<=100)
{
if(i<=60)
{
printf(BLUE"[%-100s][%d%%][%c]\r"NONE,bar,i,lable[i%4]);
}
else printf(RED"[%-100s][%d%%][%c]\r"NONE,bar,i,lable[i%4]);
fflush(stdout);
bar[i++]='#';
usleep(10000);
}
printf("\n");
return 0;
}
(7)代码写好后,输入:
make 回车
./pro_bar 回车,就可以看到效果了