进度条小程序

文章目录

铺垫

回车换行

回车和换行是两个独立的动作

回车是将光标移动到当前行的最开始(最左侧)

换行是竖直向下平移一行

在C语言中,\n是同时进行了回车换行,\r实现了回车操作

缓冲区

概述

先来看一个小现象:

实例代码:

shell 复制代码
#include<stdio.h>                                                                                                                       
#include<unistd.h>
int main()
{
	printf("hello gwj,hello Linux...\n");
    sleep(3);
    return 0;
}

运行mytest.exe执行程序,会输出hello gwj,hello Linux...,紧接着调用sleep函数,休眠三秒

如果将printf函数中\n删掉,会出现什么结果呢??


现象:先休眠三秒,在最后结束的狮虎打印出内容

这这种场景中,sleep函数和printf函数,哪一个先运行的呢??

答案是printf先运行,在休眠的这三秒时间里,printf已经执行完了,字符串被保存在缓冲区

缓冲区的概念需要在操作系统层面上理解,单纯在语言基础上是很难理解的

缓冲区是一个内存空间,当休眠的时候,字符串在缓冲区里,当程序结束return 0时,,强制冲刷缓冲区,再打印出字符串。

强制冲刷缓冲区

任何一个C语言程序都会默认打开三个流:

  • stdin:标准输入流
  • stdout:标准输出流
  • stderr比爱准错错误

可以通过fflush函数来刷新缓冲区

示例代码:

powershell 复制代码
   #include<stdio.h>
   #include<unistd.h>
   int main()
   {
     printf("hello gwj,hello Linux...");
     fflush(stdout);                                                                                                                       
     sleep(3);
   
     return 0;
 }


现象:

简单实现倒计时功能

示例代码:

powershell 复制代码
  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 {
  5   int cnt=9;
  6   while(cnt>=0)
  7   {
  8     printf("%d\r",cnt);
  9     fflush(stdout);                                                                                                                     
 10     cnt--;
 11     sleep(1);
 12   }
 13   return 0;
 14 }

效果展示:

代码解读:

倒计时使用新数字去覆盖老数字,并且都出一同一位置,因此在printf函数里不能使用\n,使用换行会让新数字在老数字下面。

正确做法是在printf函数后面使用\r回车符号,每打印一个数字,让光标回到这一行最开始的位置,这样打印出来的新数字就会覆盖旧数字。

但是\r不会冲刷缓冲区,因此每打印完一个数字调用 fflush(stdout)来冲刷缓冲区。

进度条小程序

版本一

实例代码

Processbar.h文件:

powershell 复制代码
#pragma once 

#include<stdio.h>

void ProcBar();      
powershell 复制代码
#include"Processbar.h"    
#include<string.h>    
#include<unistd.h>    
    
#define Length 101    
#define Style '#'    
    
const char *lable="|/-\\";    
    
//version1:    
void ProcBar()    
{    
  char bar[Length];    
  memset(bar,'\0',sizeof(bar));    
  int len=strlen(lable);    
    
  int cnt=0;    
  while(cnt<=100)    
  {    
    printf("[%-100s][%3d%%][%c]\r",bar,cnt,lable[cnt%len]);                                                                                 
    fflush(stdout);    
    bar[cnt]=Style;    
    cnt++;    
    usleep(20000);    
  }    
  printf("\n");    
}  

效果展示

分析

该进度条的原理是一次比一次多打印一点内容、

定义一个bar数组,通过循环,每次多打印一个字符,视觉上就会形成进度条是从右向左走

由于进度条每次打印是在同一行上,因此需要\r符号,让光标回到最开始的位置

版本二

Processbar.h文件:

powershell 复制代码
#pragma once    
    
#include <stdio.h>    
    
typedef void(*callback_t)(double, double);    

void ProcBar(double total, double current);                                                                                                 

Main.c文件:

powershell 复制代码
void download(double filesize,callback_t cb)    
{    
    double current = 0.0;    
    
    printf("download begin, current: %lf\n", current);    
    while(current <= filesize)    
    {    
        cb(filesize, current);    
        //从网络中获取数据                                                                                                                  
        usleep(100000);    
        current += bandwidth;    
    }    
    printf("\ndownload done, filesize: %lf\n",filesize);    
}    
    
int main()    
{    
    download(100*1024*1024,ProcBar);    
    download(2*1024*1024,ProcBar);    
    
    return 0;    
}    

Processbar.c文件:

powershell 复制代码
#include"Processbar.h"    
#include<string.h>    
#include<unistd.h>    
    
#define Length 101    
#define Style '#'  

//version 2                                                                   
void ProcBar(double total, double current)                                    
{                                                                             
    char bar[Length];                                                         
    memset(bar, '\0', sizeof(bar));                                           
    int len = strlen(lable);                                                  
                                                                              
    int cnt = 0;                                                              
    double rate = (current*100.0)/total;                                      
    int loop_count = (int)rate;                                               
    while(cnt <= loop_count)                                                  
    {                                                                         
        bar[cnt++] = Style;                                                   
        usleep(20000);                                                                                                                      
    }                                             
    printf("[%-100s][%.1lf%%][%c]\r", bar, rate, lable[cnt%len]);    
    fflush(stdout);    
}  

效果展示:

相关推荐
I592O92978310 分钟前
大健康裂变分销小程序开发
小程序
前端-文龙刚25 分钟前
小程序给对象赋值(双向绑定)方法之一
服务器·小程序·apache
QXH20000032 分钟前
数据结构—单链表
c语言·开发语言·数据结构
David猪大卫1 小时前
数据结构修炼——顺序表和链表的区别与联系
c语言·数据结构·学习·算法·leetcode·链表·蓝桥杯
Iceberg_wWzZ1 小时前
数据结构(Day14)
linux·c语言·数据结构·算法
微尘81 小时前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
VaporGas1 小时前
idea集成和使用Git指南
java·git·intellij-idea
计算机学姐1 小时前
基于PHP的电脑线上销售系统
开发语言·vscode·后端·mysql·编辑器·php·phpstorm
五味香2 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆2 小时前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试