Linux第一个小程序-进度条

Linux第一个小程序-进度条

\r&&\n

回车概念

换行概念

老式打字机的例子

行缓冲区概念

什么现象?

C++ 复制代码
#include <stdio.h>
 
int main()
 {
    printf("hello Makefile!\n");
    sleep(3);
    return 0;
 }

什么现象??

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

什么现象???

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

版本1:

process.h

C 复制代码
#pragma once

#include <stdio.h>

//v1
void process();

process.c

C 复制代码
#include "process.h"
#include <string.h>
#include <unistd.h>

#define SIZE 101
#define STYLE '='
// v1: 展示进度条基本功能
void process()
{
    int rate = 0;
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    const char *lable = "|/-\\";
    int len = strlen(lable);

    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", buffer, rate, lable[rate%len]);
        fflush(stdout);
        buffer[rate] = STYLE;
        rate++;
        usleep(10000);
    }

    printf("\n");
}

版本2:

makefile:

BIN=process
#SRC=$(shell ls *.c)
SRC=$(wildcard *.c)
OBJ=$(SRC:.c=.o)
CC=gcc
RM=rm -f

$(BIN):$(OBJ)
	@$(CC) $^ -o $@
	@echo "链接 $^ 成 $@"
%.o:%.c
	@$(CC) -c $<
	@echo "编译... $< 成 $@"

.PHONY:clean
clean:
	@$(RM) $(OBJ) $(BIN)

.PHONY:test
test:
	@echo $(BIN)
	@echo $(SRC)
	@echo $(OBJ)

#$(BIN):$(OBJ)
#	gcc $(OBJ) -o $(BIN)
#$(OBJ):$(SRC)
#	gcc -c $(SRC) -o $(OBJ)
#
#.PHONY:clean
#clean:
#	$(RM) $(BIN) $(OBJ)


#mytest:mytest.o
#	gcc mytest.o -o mytest
#mytest.o:test.c
#	gcc -c test.c -o mytest.o
#
#.PHONY:clean
#clean:
#	rm -f *.o mytest





#mytest:test.o
#	gcc test.o -o mytest
#test.o:test.s
#	gcc -c test.s -o test.o
#test.s:test.i
#	gcc -S test.i -o test.s
#test.i:test.c
#	gcc -E test.c -o test.i
#
#.PHONY:clean
#clean:
#	rm -f *.i *.s *.o mytest




#mytest:test.c
#	gcc test.c -o mytest
#
#.PHONY:clean
#clean:
#	rm -f mytest

process.h

C 复制代码
#pragma once

#include <stdio.h>

//v1
void process();

//v2
void FlushProcess(const char*, double total, double current);

process.c

C 复制代码
#include "process.h"
#include <string.h>
#include <unistd.h>

#define SIZE 101
#define STYLE '='

//v2: 根据进度,动态刷新一次进度条
void FlushProcess(const char *tips, double total, double current)
{
    const char *lable = "|/-\\";
    int len = strlen(lable);
    static int index = 0;
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));

    double rate = current*100.0/total;
    int num = (int)rate;

    int i = 0;
    for(; i < num; i++)
        buffer[i] = STYLE;

    printf("%s...[%-100s][%.1lf%%][%c]\r", tips, buffer, rate, lable[index++]);
    fflush(stdout);
    index %= len;

    if(num >= 100)printf("\n");
}

// v1: 展示进度条基本功能
void process()
{
    int rate = 0;
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    const char *lable = "|/-\\";
    int len = strlen(lable);

    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", buffer, rate, lable[rate%len]);
        fflush(stdout);
        buffer[rate] = STYLE;
        rate++;
        usleep(10000);
    }

    printf("\n");
}

main.c

C 复制代码
#include "process.h"
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

//函数指针类型
typedef void (*call_t)(const char*,double,double);

double total = 1024.0;
//double speed = 1.0;
double speed[] = {1.0, 0.5, 0.3, 0.02, 0.1, 0.01};

//回调函数
void download(int total, call_t cb)
{
    srand(time(NULL));
    double current = 0.0;
    while(current <= total)
    {
        cb("下载中", total, current); // 进行回调
        if(current>=total) break;
        // 下载代码
        int random = rand()%6;
        usleep(5000);
        current += speed[random];
        if(current>=total) current = total;
    }
}

void uploadload(int total, call_t cb)
{
    srand(time(NULL));
    double current = 0.0;
    while(current <= total)
    {
        cb("上传中", total, current); // 进行回调
        if(current>=total) break;
        // 下载代码
        int random = rand()%6;
        usleep(5000);
        current += speed[random];
        if(current>=total) current = total;
    }
}

int main()
{
    download(1024.0, FlushProcess);
    printf("download 1024.0MB done\n");
    download(512.0, FlushProcess);
    printf("download 512.0MB done\n");
    download(256.0,FlushProcess);
    printf("download 256.0MB done\n");
    download(128.0,FlushProcess);
    printf("download 128.0MB done\n");
    download(64.0,FlushProcess);
    printf("download 64.0MB done\n");
    uploadload(500.0, FlushProcess);
    return 0;
}

load 512.0MB done\n");

download(256.0,FlushProcess);

printf("download 256.0MB done\n");

download(128.0,FlushProcess);

printf("download 128.0MB done\n");

download(64.0,FlushProcess);

printf("download 64.0MB done\n");

uploadload(500.0, FlushProcess);

return 0;

}

复制代码
相关推荐
杨半仙儿还未成仙儿13 分钟前
linux ls -l 输出 drwxr-xr-x 2 root root 4096 Dec 5 21:48 rootTest 是什么意思
linux·运维·xr
内核程序员kevin13 分钟前
使用trace-cmd跟踪Linux内核函数:一次愉快的内核探险
linux·运维·linux内核·ftrace
moxiaoran575335 分钟前
使用Feign远程调用丢失请求头问题
运维·服务器
come-昂-35 分钟前
Zephyr2.7.0与3.7.0测试用例版本差异
linux·运维·服务器·测试用例
xiaobai12 339 分钟前
Windows 和 Ubuntu 双系统安装
linux·windows·ubuntu
快刀一哥40 分钟前
ubuntu扩展虚拟你
linux·运维·ubuntu
leo_jk41 分钟前
Linux centOS 7 安装 rabbitMQ
linux·centos·rabbitmq
qq_589568101 小时前
微信小程序怎么实现非tabbar页面显示tabbar,自定义组件实现
前端·微信小程序·小程序
Evaporator Core1 小时前
Vim 的调试与测试支持
编辑器·vim·excel
confident31 小时前
centos 常见问题处理
linux·运维·centos