进度条小程序

文章目录

铺垫

回车换行

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

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

换行是竖直向下平移一行

在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);    
}  

效果展示:

相关推荐
HABuo8 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
m0_555762908 小时前
VSCODE CLAUDE CODE
ide·vscode·编辑器
2501_916008898 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
webYin8 小时前
解决 Uni-App 运行到微信小程序时 “Socket合法域名校验出错” 问题
微信小程序·小程序·uni-app
jiayong239 小时前
Git 核心概念:Tag 与 Branch 的本质区别
git
Serene_Dream12 小时前
git 合并冲突的分支
git
我是一只puppy12 小时前
使用AI进行代码审查
javascript·人工智能·git·安全·源代码管理
esmap12 小时前
ESMAP 智慧消防解决方案:以数字孪生技术构建全域感知消防体系,赋能消防安全管理智能化升级
人工智能·物联网·3d·编辑器·智慧城市
2401_8589368813 小时前
【Linux C 编程】标准 IO 详解与实战:从基础接口到文件操作实战
linux·c语言
玄同76513 小时前
Git常用命令指南
大数据·git·elasticsearch·gitee·github·团队开发·远程工作