Linux实现:从倒计时到进度条

文章目录


1.回车与换行

2.缓冲区的概念

强制刷新可以使用冲刷函数fflush

c 复制代码
#include <stdio.h>
#include <unistd.h>
int main()
{
  printf("I am a \nhandsome man!");
  fflush(stdout);
  sleep(3);
  return 0;
}

这样就可以强制刷新出来了!

3.倒计时

c 复制代码
 1 #include <stdio.h>
  2 #include <unistd.h>
  3 
  4 int main()
  5 {
  6   int cnt=10;
  7   while(cnt>=0)
  8   {
  9     printf("%2d\r",cnt);
 10     fflush(stdout);
 11     cnt--;
 12     sleep(1);
 13   }                                                                         
 14   printf("\n");
 15   return 0;
 16 
 17 }

4.进度条(第一版无应用场景)

大制作一个这个样子的进度条:

c 复制代码
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#define Length 101
#define style '#'

const char* lable = "|/-\\";//两个\\表'\'符号,因为\n,\r有其他意思
void procbar()
{
	char bar[Length];
	memset(bar, '\0', sizeof(bar));
	int len = strlen(lable);
	int cnt = 0;
	while (cnt <= 100)
	{
		printf("[%-100s][%d%%][%c]\r", bar,cnt,lable[cnt%len]);//100s从左向右打印(默认右对齐),-100s即为左对齐、、lable[cnt%len]防止越界
		fflush(stdout);
		bar[cnt] = style;
		cnt++;
		usleep(20000);
	}
	printf("\n");
}

int main()
{	
	procbar();
	return 0;
}

5.进度条(第二版有应用场景)

我们的进度条不可能像第一版一样光一个进度条在那跑。

肯定是有相关场景的,比如要下载文件大小多少,带宽多少。。等等

源码:

makefile:


processbar.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "processbar.h"
#include <string.h>
#include <Windows.h>

#define Length 101
#define style '#'

const char* lable = "|/-\\";//两个\\表'\'符号,因为\n,\r有其他意思
//第一版本
//void procbar()
//{
//	char bar[Length];
//	memset(bar, '\0', sizeof(bar));
//	int len = strlen(lable);
//	int cnt = 0;
//	while (cnt <= 100)
//	{
//		printf("[%-100s][%d%%][%c]\r", bar,cnt,lable[cnt%len]);//100s从左向右打印(默认右对齐),-100s即为左对齐、、lable[cnt%len]防止越界
//		fflush(stdout);
//		bar[cnt] = style;
//		cnt++;
//		Sleep(1);//这里要改
//	}
//	printf("\n");
//}

//第二版本
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;//将文件大小换算为百分比制,不乘100.0的话算下来是一个小数,取整就恒为0了
	while (cnt <= loop_count)
	{
		bar[cnt] = style;
		cnt++;
		Sleep(1);
	}
	printf("[%-100s][%.1lf%%][%c]\r", bar, rate, lable[cnt % len]);//100s从左向右打印(默认右对齐),-100s即为左对齐、、lable[cnt%len]防止越界
	fflush(stdout);
	//printf("\n");
}

main.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "processbar.h"
#include<Windows.h>
double filesize = 100 * 1024 * 1024;//第一个1024是字节,1024*1024是1M,这个大小总共是100M

void download()
{
	double filesize = 100 * 1024 * 1024 * 1.0;
	double current = 0.0;//累计下载的数据量
	double bandwidth = 1024 * 1024*1.0;//带宽

	printf("下载开始,当前进程为:%lf\n", current);
	while (current <= filesize)
	{
		procbar(filesize,current);//动态打印出任意时刻的进度条
		//从网络中获取数据
		current += bandwidth;
		Sleep(1);
	}
	printf("\n下载完成,下载大小为:%lf\n", filesize);
}

int main()
{
	download();
	/*procbar(100.0,56.9);
	procbar(100.0, 99.9);		//这三个地方是指定下载到百分之多少
	procbar(100.0, 10.1);*/
	return 0;
}

processbar.h

c 复制代码
#pragma once
#include <stdio.h>

void procbar(double total, double current);
相关推荐
Nerd Nirvana23 分钟前
软考—系统架构设计(案例 | 论文)
linux·系统架构·软件工程·软考·计算机基础
勤奋的凯尔森同学1 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
丁卯4042 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
chengooooooo2 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
人间打气筒(Ada)4 小时前
MySQL主从架构
服务器·数据库·mysql
落笔画忧愁e5 小时前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
小冷爱学习!5 小时前
华为动态路由-OSPF-完全末梢区域
服务器·网络·华为
技术小齐6 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
ITPUB-微风6 小时前
Service Mesh在爱奇艺的落地实践:架构、运维与扩展
运维·架构·service_mesh
打不了嗝 ᥬ᭄6 小时前
Linux的权限
linux