Progress bar effect of C language imitating WGet

From , 3 Years ago, written in C++, viewed 224 times.
URL https://pastebin.vip/view/854f1fb6
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. //类似wget的进度条的实现,实际就是转移符\r的使用,\r的作用是返回至行首而不换行
  6. int main(int argc, char *argv[])
  7. {
  8.     unsigned len = 60;
  9.     char *bar = (char *)malloc(sizeof(char) * (len + 1));
  10.     for (int i = 0; i < len + 1; ++i)
  11.     {
  12.         bar[i] = '#';
  13.     }
  14.     for (int i = 0; i < len; ++i)
  15.     {
  16.         printf("progress:[%s]%d%%\r", bar+len-i, i+1);
  17.         fflush(stdout);//一定要fflush,否则不会会因为缓冲无法定时输出。
  18.         usleep(100000);
  19.         //sleep(1);
  20.     }
  21.     printf("\n");
  22.     return 0;
  23. }
  24. //cpp/8884

Reply to "Progress bar effect of C language imitating WGet"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website