Linux第一个小程序——进度条

乌市泽哥  金牌会员 | 2024-6-28 14:39:42 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 568|帖子 568|积分 1704

准备知识——缓冲区与换行

        先把重要结论放这:

  • 数据的输入输出不会立即到达目标地,会先呆在缓冲区。
  • 缓冲区相当于一行数据空间。
  • 回车时,光标回到开头,相当于清空原有数据。
  • 换行时,会直接将缓冲区数据输出。
  • 程序竣事时,会强制将缓冲区的数据输出。
        详情见我的另一篇文章:
缓冲区与回车换行-CSDN博客

写一个倒计时程序

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. int main()
  4. {
  5.   int i = 10;
  6.   while(i)
  7.   {
  8.     printf("%-2d\r", i);
  9.     fflush(stdout);
  10.     sleep(1);
  11.     i--;
  12.   }
  13.   return 0;
  14. }
复制代码
 %-2d,保存两位数且向左对齐
编写入门版进度条


  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. #define MAX 101
  5. #define LABEL '='
  6. int main()
  7. {
  8.   char bar[MAX];
  9.   memset(bar, '\0', sizeof(bar));
  10.   int count = 101;
  11.   while(count--)
  12.   {
  13.     bar[100 - count] = LABEL;
  14.     usleep(100000);
  15.     printf("%s\r", bar);
  16.     fflush(stdout);
  17.   }
  18.   printf("\n");
  19.   return 0;
  20. }
复制代码

多文件编写升级版进度条

Version1


main.c

  1. #include"processbar.h"
  2. int main()
  3. {
  4.   processbar();
  5.   return 0;
  6. }
复制代码
processbar.h

  1. #pragma once
  2. #include<stdio.h>
  3. #define NUM 101
  4. #define Body '='
  5. #define Head '>'
  6. void processbar();
复制代码
processbar.c

  1. #include"processbar.h"
  2. #include<unistd.h>
  3. #include<string.h>
  4. const char *loading = "|/-\";
  5. void processbar()
  6. {
  7.   char bar[NUM];
  8.   int n = strlen(loading);
  9.   memset(bar, '\0', sizeof(bar));
  10.   int cnt = 0;
  11.   while(cnt <= 100)
  12.   {
  13.     bar[cnt++] = Body;
  14.     if(cnt < 100)
  15.       bar[cnt] = Head;
  16.     printf("[%-101s][%3d%%][%c]\r", bar, cnt - 1, loading[cnt%n]);
  17.     fflush(stdout);
  18.     usleep(100000);
  19.   }
  20.   printf("\n");
  21. }
复制代码
makefile

  1. processbar:main.o  processbar.o
  2.         gcc -o $@ $^
  3. main.o:main.c
  4.         gcc -c main.c
  5. processbar.o:processbar.c
  6.         gcc -c processbar.c
  7. .PHONY:clean
  8. clean:
  9.         rm processbar main.o processbar.o
复制代码
Version2

main.c

  1. #include"processbar.h"
  2. int main()
  3. {
  4.   processbar();
  5.   return 0;
  6. }
复制代码
processbar.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<unistd.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. #include<time.h>
  7. #define NUM 101
  8. #define Body '='
  9. #define Head '>'
  10. #define FILESIZE 1024*1024*1024
  11. void processbar();
复制代码
processbar.c

  1. #include"processbar.h"
  2. char bar[NUM];
  3. const char *label = "|/-\";
  4. void processbar()
  5. {
  6.   int total = FILESIZE;
  7.   int num = 0;
  8.   srand(time(NULL)^1023);
  9.   memset(bar, '\0', sizeof(bar));
  10.   while(total)
  11.   {
  12.     usleep(100000);
  13.     int one = rand()%(1024*1024*50);
  14.     total -= one;
  15.     if(total < 0) total = 0;
  16.     double rate = ((FILESIZE - total)*1.0 / (FILESIZE)) * 100.0;
  17.     num++; num %= 4;
  18.     memset(bar, Body, sizeof(char)*((int)rate + 1));
  19.     if((int)rate < 100) bar[(int)rate] = Head;
  20.     printf("[%-101s][%6.2f%%][%c]\r", bar, rate, label[num]);
  21.     fflush(stdout);
  22.   }
  23.   printf("\n");
  24. }
复制代码
makefile

  1. processbar:main.o  processbar.o
  2.         gcc -o $@ $^
  3. main.o:main.c
  4.         gcc -c main.c
  5. processbar.o:processbar.c
  6.         gcc -c processbar.c
  7. .PHONY:clean
  8. clean:
  9.         rm processbar main.o processbar.o
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表