北冰洋以北 发表于 2024-9-12 22:59:30

Linux小步伐:进度条

目次
一、换行与回车
二、缓冲区
三、倒计期间码
四、进度条实现

实现进度条步伐,起首需要两个预备知识:
1.换行与回车
2.缓存区
一、换行与回车

我们通常使用\n作为换行符,但现实上\n是换行+回车两种效果的结合,因此我们需要区分一下换行与回车的区别:
换行是从上一行的第n个位置跳转到下一行的第n个位置;回车则是从某一行的第n个位置跳转到该行的第一个位置
\r表示回车,\r\n表示回车+换行(只有\n和\r一起使用时,\n才表示换行),\n表示回车+换行
二、缓冲区

先对比两段代码,唯一的区别就是printf函数输出时有没有加上\n
运行时,代码1会先输出内容,再等候3秒;代码2会先等候3秒,再输出内容
由此引入问题:代码2先等候3秒,但是步伐运行时一定是按次序实行代码,也就是说是printf函数先实行,那么再等候的这3秒内,printf函数输出的数据在哪里呢?
答案是:在缓冲区,printf输出的数据会被拷贝到缓冲区,在步伐运行结束的时候会冲刷缓冲区,此时printf输出的数据才会显示出来
//第一段代码
#include <stdio.h>
#include <unistd.h>
int main()
{
   printf("hello makefile\n");
   sleep(3);                                                                                                      
   return 0;
}

//第二段代码
#include <stdio.h>
#include <unistd.h>
int main()
{
   printf("hello makefile");
   sleep(3);                                                                                                      
   return 0;
} 缓冲区的三种冲刷方式:
(1)\n冲刷
printf输出时加上\n,printf输出的数据被拷贝到缓冲区,\n会主动冲刷缓冲区
(2)fflush强制冲刷
我们可以调用fflush函数强制冲刷缓冲区,包罗在头文件<unistd.h>
(3)步伐运行结束冲刷
步伐运行结束时,也会主动冲刷缓冲区
三、倒计期间码

根据上面的明白,我们可以实现一个简单的倒计期间码
C语言在输出时会默认打开3个标准输入输出流,分别为stdin、stdout、stderr,此中stdin为键盘输入,stdout和stderr都是显示器输出
#include <stdio.h>
#include <unistd.h>
int main()
{
   int cnt=9;
   while(cnt>=0)
   {                                                                           
   printf("倒计时:%d\r",cnt);                     
   fflush(stdout);            
   sleep(1);                                                                                                   
   cnt--;                                    
   }                                 
   printf("\n");//最后一行\n的作用是防止程序最后的输出倒计时:0 被命令行提示符覆盖
                //因为\r回车,光标回到了倒计时:0的前面,此时再输出命令行提示符一定会覆盖倒计时:0
                //而使用\n换行后,缓冲区的光标就会跳到 倒计时:0 的下一行,此时输出命令行提示符就不会覆盖倒计时:0
   return 0;                           
}       如果cnt初始值为10,则始终有一个0无法被覆盖,此时我们需要将%d改为%2d,使得输出字段的最小宽度为2,小于2则在左侧用空格补齐,这样就能覆盖掉0了
#include <stdio.h>
#include <unistd.h>
int main()
{
   int cnt=10;
   while(cnt>=0)
   {                                                                           
   printf("倒计时:%2d\r",cnt);                     
   fflush(stdout);            
   sleep(1);                                                                                                   
   cnt--;                                    
   }                                 
   printf("\n");
   return 0;                           
}       四、进度条实现

//progress_bar.h
void progress_bar();

//progress_bar.c
#include <stdio.h>
#include "progress_bar.h"
#include <memory.h>
#include <unistd.h>
#define out '#'
void progress_bar()
{
    char rotate = {'|', '/', '\\', '-'};
    char arr;
    memset(arr, '\0', 101);
    int cnt = 0;
    while (cnt < 100)
    {
      arr = out;
      printf("[%-100s][%d%%][%c]\r", arr, cnt + 1, rotate);
      fflush(stdout);
      usleep(20000); // usleep,单位为微秒
      cnt++;
    }
    printf("\n");
}

//main.c
#include <stdio.h>
#include "progress_bar.h"
int main()
{
    progress_bar();
    return 0;
} https://i-blog.csdnimg.cn/direct/e95eb16a71564b088e8129e76057cec1.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux小步伐:进度条