实现一个简单的进度条,从0%-100%之间显示
Linux下的输出有缓存,如果及时刷新显示, 需要调用下 fflush
#include<stdio.h>
void proc()
{
char buf[103];
memset(buf, ' ', sizeof(buf));
buf[0] = '[';
buf[101] = ']';
buf[102] = ' ';
int i = 0;
char index[6] = "-\|/ ";
while (i <= 100)
{
buf[i] = '=';
printf("%s [%d%%][%c] ", buf, i, index[i % 4]);
fflush(stdout);//刷新缓冲区
sleep(0.1);
i++;
}
printf(" ");
}
int main()
{
proc();
void proc()
{
char buf[103];
memset(buf, ' ', sizeof(buf));
buf[0] = '[';
buf[101] = ']';
buf[102] = ' ';
int i = 0;
char index[6] = "-\|/ ";
while (i <= 100)
{
buf[i] = '=';
printf("%s [%d%%][%c] ", buf, i, index[i % 4]);
fflush(stdout);//刷新缓冲区
sleep(0.1);
i++;
}
printf(" ");
}
int main()
{
proc();
return 0;
}