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;
}
相关推荐
半桔44 分钟前
【IO多路转接】高并发服务器实战:Reactor 框架与 Epoll 机制的封装与设计逻辑
linux·运维·服务器·c++·io
HABuo1 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
Howrun7772 小时前
关于Linux服务器的协作问题
linux·运维·服务器
2501_916008892 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
webYin2 小时前
解决 Uni-App 运行到微信小程序时 “Socket合法域名校验出错” 问题
微信小程序·小程序·uni-app
我在人间贩卖青春2 小时前
C++之多重继承
c++·多重继承
m0_736919102 小时前
C++代码风格检查工具
开发语言·c++·算法
小白同学_C2 小时前
Lab3-page tables && MIT6.1810操作系统工程【持续更新】
linux·c/c++·操作系统os
十年磨一剑~3 小时前
Linux程序接收到sigpipe信号崩溃处理
linux
geshifei3 小时前
Sched ext回调3——select_cpu(linux 6.15.7)
linux·ebpf