Teach you step by step how to write a progress bar applet under Linux (source code attached)

Show results

Screen Recording 2023

1. Create files

mkdir ProgressBar   //在当前目录下,建立新的目录
cd ProgressBar      //进入这个目录
touch main.c  makefile  progressbar.c  progressbar.h //在ProgressBar这个目录建立这几个文件

After entering the ProgressBar directory, use the ls command to check whether the creation is successful.

2. Write makefile

The purpose of writing a makefile is to use the make command to build our .c file and generate an executable program.

progressbar:progressbar.c main.c
	gcc -o $@ $^

.PHONY:clean
clean:
	rm -f progressbar

3. Source code

Copy and paste the source code into our corresponding file

1. Header file.h

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#define DOWNLOAD 1024*1024*1024 //1GB,下载的总大小
#define MAX 102   //str数组的长度
#define S '='      //数组的进度表示符号
void ProgressBar();   //函数声明

Use the vim command to open the header file first

vim progressbar.h

Press lowercase i to enter insert mode (Insert) and copy directly

Press esc again and enter directly: wq (must be entered manually, cannot be copied here, and there must be a semicolon)

esc lets us enter the bottom line mode. Press :wq to save and exit. Then follow the same steps to complete the copy of the following files.

2. Write the .c file with function definition

#include "progressbar.h"
char str[MAX] = "\0"; //这里要定义一个全局字符数组,防止每次调用这个函数都要开辟空间,销毁空间,影响效率
char *ch = ". ";      // 这个字符串是为了表示下载一直在进行中,即使下载卡顿,也会转动,表示一直在下载;这里ch的字符串可以自己设定转动的字符,我这里设置的是.和空格,表示闪烁
int i = 0;            // 全局变量i,控制表示下载进行中的转动速度
void Init(int p)      //初始化进度条的内容,根据下载百分比来的,用图形表示当前的进度
{
	char *cur = str;
	for(int j = 0; j < p; j++)
	{
		str[j] = S;
	}
}
void ProgressBar(double percentage) //完成打印进度条的工作
{
	int len = strlen(ch);     //len是ch字符串的长度,让ch这个字符串一直在循环输出
    int p = (int)percentage;   //这里强转一下percentage是因为,p是表示str数组的下标
	if(percentage >= 100)      //如果下载百分比达到100.或者是大于100,就让他是100,符合我们日常逻辑
		percentage = 100;
	Init(p);                   //得知下载百分比之后,进入初始化str
    printf("[%-100s][%.2f%%][%c]\r", str, percentage, ch[i%len]);   //这里就是打印进度条
    fflush(stdout); //强制刷新缓冲区
	i++;           //让i不断增大
	i %= len;        //但是不能超过ch的长度
}

3. The .c file of the main function (the file called by the function)

#include "progressbar.h"
void DownLoad()
{
    srand(time(NULL)^1023);  //设置一个随机数,用来分配速度
    int download = DOWNLOAD; //下载总量,可以自己设定
    int alreadydown = 0;     //已经下载的大小
    double percentage = 0;   //下载百分比
    int v = 0;               //下载速度
    while(alreadydown < download) //循环条件就是我们下载量是否大于总大小
    {
        usleep(100000); //休眠时间
        v = rand()%(1024*1024*40); //从随机数赋值速度,%后面的数字,是为了不让这个速度超过这个数字
        alreadydown += v;          //每次下载累积
        percentage = alreadydown*100.0 / download; //下载百分比*100,让我们更好查看,否则下完百分比是0.99,1.00等不好看
        ProgressBar(percentage);  //调用函数,这里函数主要完成打印进度条
    }
    printf("\n安装完毕!\n");
}
int main()
{
    DownLoad();
    return 0;
}

4. Operation

After the copy is completed, directly use the following command

make

Then do ls and you will see that an executable program is generated

Run this executable program

./progressbar

At this point we can see the progress bar being generated~

5. Processing

If you want to delete all files

rm -rf *
cd ..
rm -rf ProgressBar

Guess you like

Origin blog.csdn.net/2302_76941579/article/details/134625925