知识了解
\r :回车,将光标回到本行最前面
\n: 换行,另起一行光标不一定在最前面
进度条的编写我们要保证进度条的增长,百分数进度的增加。
我们可以利用\r的特点在一行内显示进度条的增长,不断刷新缓冲区,输出其中的内容。
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define SIZE 101
#define STYLE '='
#define ARR '>'
void process()
{
char bar[SIZE];
memset(bar,'\0',sizeof(bar));
char arr[4]={'-','|','/','\\'};
int i=0;
while(i<=100)
{
printf("[%-100s][%d%%][%c]\r",bar,i,arr[i%4]);
bar[i++]=STYLE;
if(i!=100)
{
bar[i]=ARR;
}
fflush(stdout);
usleep(100000);
}
printf("\n");
}