Linux小程序:进度条
在实现小程序前我们要弄清楚:
1.缓冲区;
2.回车与换行。
缓冲区:
分别用gcc来编译下面两个程序:
程序一:
#include
int main()
{
printf("hello Makefile!\n");
sleep(3);
return 0;
}
程序二:
#include
int main()
{
printf("hello Makefile!\n");
sleep(3);
return 0;
}
可以看到打印程序二没有 \n ’后,没有在该打印的时候打印,反而在结束后才出现,
这说明peintf的运行结果hello Makefile被提起保存在了某个地方,当程序结束时,hello Makefile才打印出来,这个地方就是缓冲区!
回车与换行:
通过老式键盘就会发现其实Enter是包含换行和回车的意思:
回车:就是到一行的开头;
换行(' \r '):切换到当前位置的下一行;
有了以上的知识储备我们就可以开始写Linux的第一个程序了!!
进度条:
由于没有了' \n '无法刷新缓冲区,所以还需要了解一个强制刷新的函数:
头文件为:#include
函数:fflush()
此时我们先来写个倒计时:
进度条,启动!!
首先需要创建makefile文件实现多文件之间的自动化构建:
makefile:
1 my-test:game.o main.o 2 gcc -o $@ $^ 3 main.o:main.c 4 gcc -c main.c 5 game.o:game.c 6 gcc -c game.c 7 8 .PHONY:clean 9 clean: 10 rm -f my-test game.o main.o
game.h:
1 #include 2 #include 3 #include 4 #include 5 6 #define Head '>' 7 #define MAX 102 8 #define CHAR '=' 9 10 //进度条实现 11 void Progress(double n);
game.c:
1 #include "game.h" 2 //下载在进行图标 3 const char*arr1="|/-\\"; 4 //进度条数组 5 char arr2[MAX] = {}; 6 void Progress(double n) 7 { 8 //char arr1[4] = "|/-\"; 9 static int count = 0; 10 int count1 = count % 4; 11 count++; 12 //下载进度未达到1%需要一个箭头表示 13 if(n