ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Linux第一个小程序——进度条

2026/9/10 21:17:12 拓冰建站 浏览量
Linux第一个小程序——进度条

目录

回车和换行

缓冲区

设计倒计时

进度条(多文件操作)

Version1:进度条

Version2:应用场景+进度条

Processbar.h

Processbar.c

Main.c

Makefile 

Version3:升级彩色进度条


回车和换行

回车\r:'r' 回车,回到当前行的行首,而不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖。

换行\n:'n' 换行,换到当前位置的下一行,而不会回到行首。(满足回车/换行两种功能)

缓冲区

  1 #include<stdio.h>2 #include<unistd.h>//sleep3 int main()4 {5   printf("hello linux,hello word");                                                                          6   sleep(3);7   return 0;8 }
  1 #include<stdio.h>2 #include<unistd.h>//sleep3 int main()4 {5   printf("hello linux,hello word\n");                                                                          6   sleep(3);7   return 0;8 }

屏幕录制 2024-01-27 194734

执行上面两段代码(它们的区别是是否包含\n)

我们清晰的发现:

  • 带有\n的就直接出现在了显示器(屏幕)上
  • 没有带\n的程序结束之后才回显示在终端上

结论:包括\n在内的之前全部字符串全部刷新到我们的显示器上(行刷新)

刷新的方式:

  •  \n
  • 缓冲区满了
  • 程序结束                     
  • 强制刷新fflush   fflush(stdout)
  • 头文件#include<unistd.h> //sleep所需要的头文件包含【Linux系统编程】Linux的系统库中的unistd.h头文件的作用-CSDN博客
  • fflush - C++ Reference (cplusplus.com)
  • stdout - C++ Reference (cplusplus.com)
//输出流--显示器/终端
extern FILE * stdout;
extern FILE * stderr;
//输入流--键盘
extern FILE * stdin;
//开机默认打开三个流(输入/输出流)
  //修改之后1 #include<stdio.h>2 #include<unistd.h>//sleep3 int main()4 {5   printf("hello linux,hello word");6   fflush(stdout);                                                                                            7   sleep(3);                                                                                8   return 0;                                                                                9 } 

为什么是行刷新??缓冲区的存在又是为了什么?立即刷新?

  • 行刷新既考虑到了用户的读取(用户一般是一行一行读取),又考虑到了效率问题。
  • 缓冲区的存在就是为了提高效率。
  • 立即刷星就是我们在终端/显示器从键盘输入文字字符,立即出现在用户面前。
  • 应用思考:Linux上输入密码不显示的原因。 

设计倒计时

  1 #include<stdio.h>2 #include<unistd.h>3 int main()4 {5   int cnt=10;6   while(cnt>=0)7   {8     printf("倒计时:%2d\r",cnt);//2d  9     fflush(stdout);//刷新数据10     cnt--;11     sleep(1);//休眠12   }13   printf("\n");                                                                                              14   return 0;15 }
  • 原理:同一个位置用不同的数字做刷新,所以是动态的变化的 。
  • \r回车,光标回到最开始覆盖前面数据,历史数据在缓冲区已经被fflush刷新到终端上了
  • 2d 是两个字符占位符,我们输入123,输入的是字符1 字符2 字符3 所以10需要两个占位符。
  • 显示器是字符设备。
  • sleep函数引用#include<unistd.h>

最终呈现效果: 

屏幕录制 2024-01-28 122350

进度条(多文件操作)

  • Processbar.h 包含头文件&函数声明
  • Processbar.c函数实现
  • Main.c函数调用/测试
  • Makefile自动化项目构建
  • Makefile里面不需要包含头文件Processbar.h,因为头文件在当前目录下且Processbar.c和Main.c都包含了Processbar.h在自动化构建编译时,回展开头文件,所以不需要!!

 测试一下:

Version1:进度条

#include"Processbar.h"2 //void test()3 //{4 //  printf("this is a test\n");5 //  printf("this is a test\n");6 //  printf("this is a test\n");7 //  printf("this is a test\n");8 //  printf("this is a test\n");9 //  printf("this is a test\n");10 //}     11 #define length 101 //包括\012 #define DataType '#'13 const char*MoveType = "|/-\\";14 void ProcBar()15 {16   char Bar[length];//缓存空间17   memset(Bar,'\0',sizeof(Bar));18   int cnt=0;19   while(cnt<=100)20   {21     printf("[%-100s][%3d%%][%c]\r",Bar,cnt,MoveType[cnt%strlen(MoveType)]);22     fflush(stdout);23     Bar[cnt++]=DataType;24     usleep(20000);//0.02s=20000微秒25   }26   printf("\n");27 }

屏幕录制 2024-01-28 151404

注释:

  • memset设置为了遇到\0停止打印,如果不清空全是随机值打印。
  • 单位换算
  • printf一般是右对齐,加上符号-左对齐
  • %% 打印出 %
  • \\ 是反斜杠\的字面常量
  • 占位符100% 3%
  • 取余是为了防止数组越界!!
  • 强制刷新fflush(stdout)
  • 设计\-/\\ 进度条一直在加载/下载,但是因为网速等原因,卡住不动,但是此刻用户又想看到一个进度条在下载的动态效果


 Version2:应用场景+进度条

  • 下载文件filesize
  • 当前下载量currentsize
  • 带宽/网速bandwidth
  • 根据网速/宽带的不同,使其下载文件的速度也有差别,进度条的快慢也有差别
  • 单位换算1兆=1024*1024字节
  • 占比currentsize/filesize*100.0
  • 强制类型转化
  • 浮点数打印lf
  • 回调函数&函数指针复习回顾

Processbar.h

  1 #pragma once //防止头文件被重复包含2 #include<stdio.h>3 #include<unistd.h>4 #include<string.h>5 //extern 表示此函数/变量在其他文件别处定义6 //extern void test();7 typedef void(*callbact_t)(double,double);8 void ProcBar(double filesize,double currentsize);9 // void Download(double filesize,callbact_t cb); 

Processbar.c

    #include"Processbar.h"34 #define length 101 //包括\035 #define DataType '#'36                                                                                                              37 const char*MoveType = "|/-\\";38 void ProcBar(double filesize,double currentsize)39 {40   char Bar[length];//缓存空间41   memset(Bar,'\0',sizeof(Bar));42   int cnt=0;43   double rate=currentsize*100.0/filesize;//占比44   int count=(int)rate;45   while(cnt <= count)46   {47    // printf("[%-100s][%.1lf%%][%c]\r",Bar,rate,MoveType[cnt%strlen(MoveType)]);48    // fflush(stdout);49     Bar[cnt++]=DataType;50    // usleep(20000);//0.02s=20000微秒51   }52     printf("[%-100s][%.1lf%%][%c]\r",Bar,rate,MoveType[cnt%strlen(MoveType)]);53     fflush(stdout);54 }

Main.c

  1 #include"Processbar.h"2 //设置一个场景3 //double filesize=100*1024*1024*1.0;//总的文件大小4 //double currentsize=0.0;//目前下载的文件数5 double bandwidth=1024*1024*1.0;6 void Download(double filesize,callbact_t cb)                                                                 7 {8   double currentsize=0.0;9     printf("Download begin,currentsize:%lf\n",currentsize);10   while(currentsize <= filesize)11   {12     cb(filesize,currentsize);13     usleep(20000);14     currentsize+=bandwidth;15   }16      printf("\nDownload done,currentsize:%lf\n",currentsize);17 }18 int main()19 {20   Download(100*1024*1024*1.0,ProcBar);21  //ProcBar();22  //test();23   return 0;24 }

Makefile 

1 Processbar:Processbar.c Main.c2   @gcc -o $@ $^3 .PHONY:clean4 clean:5   @rm -f Processbar 

屏幕录制 2024-01-29 140123


Version3:升级彩色进度条

#include<stdio.h>
#include<unistd.h>
#include<string.h>int main()
{int k=0;int j=0;int color[]={4,3,2,5,6};char bar[102];memset(bar,0,sizeof(bar));const char* str="|/-\\";for(;k<=100;++k){printf("\033[3%dm[%-100s]\033[0m\033[33m[%d%%]\033[0m[%c]\r",                  color[j], bar, k, str[k % 4]);fflush(stdout);bar[k]='*';//每20%变色一次if(k%20==0){++j;}usleep(7000);}printf("\n");return 0;}

🙂感谢大家的阅读,若有错误和不足,欢迎指正