Hello! 欢迎来到小浪云!


Linux探秘坊——-4.进度条小程序


1.缓冲区代码语言:JavaScript代码运行次数:0运行复制

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

执行此代码后,会 先停顿两秒,再打印出hello bite,但是明明打印在sleep前面,为什么会后打印呢?

因为:

printf执行完了后,打印的内容被存到了缓冲区程序快结束时缓存区的内容再打印到显示器

奇妙的事情发生了:

代码语言:javascript代码运行次数:0运行复制

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

如果我们使用这样的代码,会 先打印出hello bite,再停顿两秒,为啥捏?

可以发现和上面的代码对比多了一个/n

显示器有一种刷新策略,叫 行刷新而 /n正好代表着换行,所以加了/n的内容会 直接从缓存区打印到显示器上,不用等程序结束

那么我们有没有办法 不用 也能先打印呢? 包有的:

代码语言:javascript代码运行次数:0运行复制

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

这里的fflush(stdout)就起到了 刷新的作用2.进度条(V1)1.process.h

Linux探秘坊——-4.进度条小程序

2.main.c

Linux探秘坊——-4.进度条小程序

3.process.c(重点)

Linux探秘坊——-4.进度条小程序

代码语言:javascript代码运行次数:0运行复制

#include"process.h"    2 #include<string.h>    3 #include<unistd.h>    4     5 #define size 101//需要考虑所以不是100    6 #define style '#'    7     8 void process()    9 {   10   int rate =0;   11   char num[size];   12   memset(num,0,sizeof(num));//这里是让num全部置空W&gt; 13   char* lable="|/-";//这里是模拟进度条后的圈圈   14   int length=strlen(lable);   15   while(rate3.进度条(V2)1.process.h<figure class=""><img src="https://img.php.cn/upload/article/001/503/042/174502910754401.jpg" alt="Linux探秘坊-------4.进度条小程序"></figure>2.process.c(重点)<figure class=""><img src="https://img.php.cn/upload/article/001/503/042/174502910760192.jpg" alt="Linux探秘坊-------4.进度条小程序"></figure>代码语言:javascript<i class="icon-code"></i>代码运行次数:<!-- -->0<svg xmlns="http://www.w3.org/2000/svg" width="16"    style="max-width:90%" viewbox="0 0 16 16" fill="none"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg>运行<svg width="16" height="16" viewbox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor"></path></svg>复制<pre class="prism-token token line-numbers javascript">void flushprocess(const char*tips, double total,double current)   29 {   30   const char *lable="|/-";   31   int len =strlen(lable);   32   static int index=0;   33   char num[size];   34   memset(num,0,sizeof(num));//将num置空   35    36   double rate=current*100.0/total;//计算下载进度   37   int n=(int)rate;//用下载进度确定有多少个#                                                                                                     38    39   int i=0;   40   for(;i<n num printf fflush>   45   index %=len;   46   if(n&gt;=100)printf(" ");   47 }</n>

3.main.c(重点)

Linux探秘坊——-4.进度条小程序

代码语言:javascript代码运行次数:0运行复制

#include"process.h"  3 #include<unistd.h>  4 #include<time.h>  5 #include<stdlib.h>  6   7 typedef void (*call)(const char*, double,double);//定义函数指针类型  8   9   10  11 double speed[]={5.0,0.5,0.3,0.02,0.1,0.01};//模拟网速的波动    12                                                                13 void download(int total,call cb)                               14 {                                                              15   srand(time(NULL));//设置种子                                 16   double current =0.0;                                         17   while(current=total)break;                                   21     int random=rand()%6;//随机匹配网速                         22     usleep(5000);                                              23     current+=speed[random];//加上每次下载的进度                24     if(current&gt;=total)current=total;//有可能总和超过total,这样就没法显示100%,所以需要改为total 25   }                                                            26 }                                                              27                                                                28                                                                29                                                                30                                                                31 int main()                                                     32 {                                                              33    download(1024.0,flushprocess);//传递文件大小和函数地址                                                                                       34    printf("下载 1024.0MB 完成 ");                                  35    download(512.0,flushprocess);                                    36    printf("下载 512.0MB 完成 ");                                   37    download(256.0,flushprocess);                                    38    printf("下载 256.0MB 完成 ");                                   39    download(128.0,flushprocess);                                    40    printf("下载 128.0MB 完成 ");                                   41                                                                     42    return 0;                                                        43 }     </stdlib.h></time.h></unistd.h>

效果如下:

Linux探秘坊——-4.进度条小程序

相关阅读