The C language implements a progress bar effect similar to wget
- 2020-04-02 03:01:59
- OfStack
This article focuses on the implementation of a progress bar similar to wget, which is actually the use of the transition character \r, which returns to the beginning of a line without breaking
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//The implementation of a progress bar like wget is actually the use of the transition character r, which returns to the beginning of the line without breaking the line
int main(int argc, char *argv[])
{
unsigned len = 60;
char *bar = (char *)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len + 1; ++i)
{
bar[i] = '#';
}
for (int i = 0; i < len; ++i)
{
printf("progress:[%s]%d%%r", bar+len-i, i+1);
fflush(stdout);//Make sure you have fflush, otherwise you won't be unable to time the output because of buffering. < br / >
usleep(100000);
//sleep(1);
}
printf("n");
return 0;
}
That's all for this article, I hope you enjoy it.