C++系统编程篇——Linux第一个小程序--进度条

(1)先引入一个概念:行缓冲区

\r\n

\r表示回车

\n表示回车并换行

①代码一
 
  1. #include<stdio.h>

  2. #include<unistd.h>

  3. int main()

  4. {

  5. printf("hello Makefile!\n");

  6. sleep(3);

  7. return 0;

  8. }

打印出了hello Makefile!,然后三秒后结束了运行

②代码二
 
  1. #include<stdio.h>

  2. #include<unistd.h>

  3. int main()

  4. {

  5. printf("hello Makefile!");

  6. sleep(3);

  7. return 0;

  8. }

什么也没显示,程序结束后打印出了hello Makefile!,同时在同一行出现了命令提示符

③代码三
 
  1. #include<stdio.h>

  2. #include<unistd.h>

  3. int main()

  4. {

  5. printf("hello Makefile!");

  6. fflush(stdout);

  7. sleep(3);

  8. return 0;

  9. }

打印出了hello Makefile!,然后三秒后结束了运行,同时在同一行出现了命令提示符

这是因为 printf("hello Makefile!"); 没有包含换行符,缓冲区的内容不会立即被刷新

printf 会先将数据写入到一个缓冲区中,只有在遇到换行符(\n),缓冲区满了,或者程序正常结束时,缓冲区的内容才会被真正输出到终端。

(2)进度条项目

根据上述特性,可以写出进度条,用字符#表示进度,打印出以后进行回车不换行,每次多加一个#,就能够实现进度条在加载的效果,而每次变更打印的内容为数字的话,就可以有进度在变更的效果。

<Processbar.h>

 
  1. #pragma once

  2. #include<stdio.h>

  3. typedef void(*callback_t)(double,double);

  4. void ProcBar(double total,double current);

<Processbar.c>

 
  1. #include"Processbar.h"

  2. #include<string.h>

  3. #include<unistd.h>

  4. #define length 101

  5. #define Style '#'

  6. // version 1

  7. const char *lable = "|/-\\";

  8. //void ProcBar()

  9. //{

  10. // char bar[length];

  11. // memset(bar,'\0',sizeof(bar));

  12. //

  13. // int len = strlen(lable);

  14. // int cnt = 0;

  15. // while(cnt <= 100)

  16. // {

  17. // printf("[%-100s][%3d%%][%c]\r",bar,cnt,lable[cnt%len]);

  18. // fflush(stdout);

  19. // bar[cnt++] = Style;

  20. // usleep(60000);

  21. // }

  22. // printf("\n");

  23. //

  24. //}

  25. //

  26. // version 2

  27. void ProcBar(double total,double current)

  28. {

  29. char bar[length];

  30. memset(bar,'\0',sizeof(bar));

  31. int len = strlen(lable);

  32. double rate = (current*100.0)/total;

  33. int cnt = 0;

  34. int loop_count = (int)rate;

  35. while(cnt <= loop_count)

  36. {

  37. bar[cnt++] = Style;

  38. //usleep(10000);

  39. }

  40. printf("[%-100s][%.1lf%%][%c]\r",bar,rate,lable[cnt%len]);

  41. fflush(stdout);

  42. }

<Main.c>

 
  1. #include"Processbar.h"

  2. #include<stdio.h>

  3. #include<unistd.h>

  4. //download

  5. void download(double filesize,callback_t cb)

  6. {

  7. double current = 0.0;//已下载量

  8. double bandwidth = 1024*1024*1.0;//带宽

  9. printf("download begin, current:%lf\n",current);

  10. while(current<=filesize)

  11. {

  12. cb(filesize,current);

  13. //从网络中获取数据

  14. current+=bandwidth;

  15. usleep(100000);

  16. }

  17. printf("\ndownload done, filesize:%.3lf\n",filesize);

  18. }

  19. int main()

  20. {

  21. download(100*1024*1024,ProcBar);

  22. download(2*1024*1024,ProcBar);

  23. download(200*1024*1024,ProcBar);

  24. download(400*1024*1024,ProcBar);

  25. download(50*1024*1024,ProcBar);

  26. //ForTest();

  27. // ProcBar(100.0,56.9);

  28. // ProcBar(100.0,1.0);

  29. // ProcBar(100.0,99.9);

  30. // ProcBar(100.0,100);

  31. // return 0;

  32. }

  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值