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

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

\r\n

\r表示回车

\n表示回车并换行

①代码一

#include<stdio.h>
#include<unistd.h>                                                                                        
int main()
{
    printf("hello Makefile!\n");
    sleep(3);
    return 0;
}

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

②代码二

#include<stdio.h>
#include<unistd.h>                                                                                        
int main()
{
    printf("hello Makefile!");
    sleep(3);
    return 0;
}

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

③代码三

#include<stdio.h>
#include<unistd.h>                                                                                        
int main()
{
    printf("hello Makefile!");
    fflush(stdout);
    sleep(3);
    return 0;
}

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

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

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

(2)进度条项目

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

<Processbar.h>

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

void ProcBar(double total,double current);

<Processbar.c>

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

// version 1
const char *lable = "|/-\\";
//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;
//    usleep(60000);
//  }                                                                                                                    
//  printf("\n");
//
//}
//

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

<Main.c>

cpp 复制代码
#include"Processbar.h"
#include<stdio.h>
#include<unistd.h>
//download

void download(double filesize,callback_t cb)
{
    double current = 0.0;//已下载量
    double bandwidth = 1024*1024*1.0;//带宽
    
    printf("download begin, current:%lf\n",current);
    
    while(current<=filesize)
    {
        cb(filesize,current);
        //从网络中获取数据
        current+=bandwidth;
        usleep(100000);
    }                                                                                                
    printf("\ndownload done, filesize:%.3lf\n",filesize);
}
int main()                                                                                                               
{
    download(100*1024*1024,ProcBar);
    download(2*1024*1024,ProcBar);
    download(200*1024*1024,ProcBar);
    download(400*1024*1024,ProcBar);
    download(50*1024*1024,ProcBar);
    
    //ForTest();
    // ProcBar(100.0,56.9);
    // ProcBar(100.0,1.0);
    // ProcBar(100.0,99.9);
    // ProcBar(100.0,100);
    // return 0;
}
相关推荐
IT利刃出鞘21 分钟前
SecureCRT--使用sftp上传和下载文件
linux·运维·服务器
努力学习的木子2 小时前
uniapp如何隐藏默认的页面头部导航栏,uniapp开发小程序如何隐藏默认的页面头部导航栏
前端·小程序·uni-app
Java资深爱好者3 小时前
如何在std::map中查找元素
开发语言·c++
踩着阴暗的自己向上爬3 小时前
Day05-04-持续集成总结
linux·运维·ci/cd
qyhua4 小时前
Linux内网端口转公网端口映射
linux·运维·服务器
j.king6 小时前
开源GTKSystem.Windows.Forms框架让C# winform支持跨平台运行
linux·c#·gtk
安步当歌6 小时前
【FFmpeg】av_write_trailer函数
c语言·c++·ffmpeg·视频编解码·video-codec
stackY、7 小时前
【Linux】:程序地址空间
linux·算法
R语言爱好者8 小时前
如何查看程序是否在运行-Linux
linux
shuguang258008 小时前
C++ 函数高级——函数重载——基本语法
开发语言·c++·visualstudio