Linux第一个小程序-进度条
创始人
2024-05-11 22:28:01
0

目录

\r&&\n

行缓冲区概念

倒计时程序

进度条代码


\r&&\n

回车概念 换行概念
  • \n
[root@VM-12-17-centos lesson8]# touch test.c
[root@VM-12-17-centos lesson8]# touoch Makefile
bash: touoch: command not found
[root@VM-12-17-centos lesson8]# touch Makefile
[root@VM-12-17-centos lesson8]# vim Makefile
[root@VM-12-17-centos lesson8]# ll
total 4
-rw-r--r-- 1 root root 71 Jan 13 21:43 Makefile
-rw-r--r-- 1 root root  0 Jan 13 21:40 test.c
[root@VM-12-17-centos lesson8]# vim test.c
[root@VM-12-17-centos lesson8]# make
gcc -o mytest test.c
[root@VM-12-17-centos lesson8]# ls
Makefile  mytest  test.c
[root@VM-12-17-centos lesson8]# ./mytest
hello world
[root@VM-12-17-centos lesson8]# cat test.c
#include
int main()
{printf("hello world\n");return 0;
}
  • \r
[root@VM-12-17-centos lesson8]# vim test.c
[root@VM-12-17-centos lesson8]# make
gcc -o mytest test.c
[root@VM-12-17-centos lesson8]# cat test.c
#include
int main()
{printf("hello world\r");return 0;
}
[root@VM-12-17-centos lesson8]# ./mytest
[root@VM-12-17-centos lesson8]# make clean
rm -f mytest
  • \r\n
    [root@VM-12-17-centos lesson8]# vim test.c
    [root@VM-12-17-centos lesson8]# make clean
    rm -f mytest
    [root@VM-12-17-centos lesson8]# cat test.c
    #include
    int main()
    {printf("hello world\r\n");return 0;
    }
    [root@VM-12-17-centos lesson8]# make 
    gcc -o mytest test.c
    [root@VM-12-17-centos lesson8]# ./mytest
    hello world
    

行缓冲区概念

#include 
int main()
{printf("hello world\n");sleep(3);return 0;
}

什么现象?        直接打印hello world,好像没有执行sleep

#include 
int main()
{printf("hello world");sleep(3);return 0;
}

什么现象??    先不显示,隔一段时间再打印,好像是先执行了sleep,再执行printf?

        并不是!!一定是先执行printf(从上到下),再执行sleep,sleep时,hello world字符串没有被刷新,数据在sleep期间被保存起来了。

        为什么\n,数据就显示出来了呢?缓冲区有自己的刷新策略,包括行缓冲

        我们也可以自主刷新,使用一定的函数帮助我们实现自主刷新

#include 
int main()
{printf("hello world");fflush(stdout);sleep(3);return 0;
}

        \r不显示的情况,其光标到达hello world的后一位,遇到\r回到此行最前面,Xshell接着打印[root@VM-12-17-centos lesson8]将hello world覆盖了,为了使其效果明显,我们改变自主刷新的位置

修改前

#include
int main()
{printf("hello world\r");sleep(3);fflush(stdout);return 0;
}

修改后

#include 
int main()
{printf("hello world\r");fflush(stdout);sleep(3);return 0;
}

倒计时程序

根据上述的\r\n的特性,我们编写一个倒计时程序
#include
int main()
{int i=9;for(;i>=0;i--){printf("%d\n",i);                                                                                             sleep(1);}return 0;
}
我们尝试用\r,其字符在缓冲区存放,并未刷新,所以并不显示
#include
int main()
{int i=9;for(;i>=0;i--){printf("%d\r",i);                                                                                             sleep(1);}return 0;
}

         

        我们使用函数对其刷新,方便看倒计时

#include
int main()
{int i=9;for(;i>=0;i--){printf("%d\r",i);fflush(stdout);                                                                                             sleep(1);}printf("\n"); return 0;
}

        我们尝试从10开始倒计时,键盘设备称为字符设备,是按字符打印、显示,所以从10开始倒计时,我们不能单纯地修改i的初始值为10,我们可以%2d去预留两个字符

#include
int main()
{int i=10;for(;i>=0;i--){printf("%2d\r",i);fflush(stdout);                                                                                             sleep(1);}printf("\n"); return 0;
}

进度条代码

样式

新建

[root@VM-12-17-centos lesson8]# mkdir proc
[root@VM-12-17-centos lesson8]# cd proc
[root@VM-12-17-centos proc]# touch proc.c
[root@VM-12-17-centos proc]# touch proc.h
[root@VM-12-17-centos proc]# touch main.c

main.c内

 #include "proc.h"int main(){process();return 0;}   

 proc.h内

[root@VM-12-17-centos proc]# vim proc.h
[root@VM-12-17-centos proc]# cat proc.h
#pragma once #include ​extern void process();

proc.c内

#include "proc.h"
#include
#include#define SIZE 102
#define STYLE '='
#define ARR '>'// "|/-\\"
void process()
{const char *lable = "|/-\\";char bar[SIZE];memset(bar,'\0',sizeof(bar));int i=0;while(i<=100){printf("[%-100s][%d%%][%c]\r",bar,i,lable[i%4]);fflush(stdout);bar[i++]=STYLE;if(i!=100)bar[i]=ARR;usleep(100000);}printf("\n");
}

Makefile

[root@VM-12-17-centos proc]# touch Makefile
[root@VM-12-17-centos proc]# vim Makefile
[root@VM-12-17-centos proc]# cat Makefile
myprocess:main.c proc.cgcc -o myprocess main.c proc.c.PHONY:clean
clean:rm -f myprocess

相关内容

热门资讯

美国不提安卓系统华为,迈向自主... 华为与美国:一场关于技术、市场与政策的较量在当今这个数字化的世界里,智能手机已经成为我们生活中不可或...
安卓系统怎么打开ppt,选择文... 你有没有遇到过这种情况:手里拿着安卓手机,突然需要打开一个PPT文件,却怎么也找不到方法?别急,今天...
谷歌退回到安卓系统,探索创新未... 你知道吗?最近科技圈可是炸开了锅,谷歌竟然宣布要退回到安卓系统!这可不是一个简单的决定,背后肯定有着...
安卓系统待机耗电多少,深度解析... 你有没有发现,手机电量总是不经用?尤其是安卓系统,有时候明明没怎么用,电量就“嗖”的一下子就下去了。...
小米主题安卓原生系统,安卓原生... 亲爱的手机控们,你是否曾为手机界面单调乏味而烦恼?想要给手机换换“衣服”,让它焕然一新?那就得聊聊小...
voyov1安卓系统,探索创新... 你有没有发现,最近你的手机是不是变得越来越流畅了?没错,我要说的就是那个让手机焕发青春的Vivo V...
电脑刷安卓tv系统,轻松打造智... 你有没有想过,家里的安卓电视突然变得卡顿,反应迟钝,是不是时候给它来个“大保健”了?没错,今天就要来...
安卓系统即将要收费,未来手机应... 你知道吗?最近有个大消息在科技圈里炸开了锅,那就是安卓系统可能要开始收费了!这可不是开玩笑的,这可是...
雷凌车载安卓系统,智能出行新体... 你有没有发现,现在的汽车越来越智能了?这不,我最近就体验了一把雷凌车载安卓系统的魅力。它就像一个聪明...
怎样拍照好看安卓系统,轻松拍出... 拍照好看,安卓系统也能轻松搞定!在这个看脸的时代,拍照已经成为每个人生活中不可或缺的一部分。无论是记...
安卓车机系统音频,安卓车机系统... 你有没有发现,现在越来越多的汽车都开始搭载智能车机系统了?这不,咱们就来聊聊安卓车机系统在音频方面的...
老苹果手机安卓系统,兼容与创新... 你手里那台老苹果手机,是不是已经陪你走过了不少风风雨雨?现在,它竟然还能装上安卓系统?这可不是天方夜...
安卓系统7.dns,优化网络连... 你有没有发现,你的安卓手机最近是不是有点儿“慢吞吞”的?别急,别急,让我来给你揭秘这可能与你的安卓系...
安卓手机系统怎么加速,安卓手机... 你有没有发现,你的安卓手机最近变得有点“慢吞吞”的?别急,别急,今天就来给你支几招,让你的安卓手机瞬...
小米note安卓7系统,探索性... 你有没有发现,手机更新换代的速度简直就像坐上了火箭呢?这不,小米Note这款手机,自从升级到了安卓7...
安卓和鸿蒙系统游戏,两大系统游... 你有没有发现,最近手机游戏界可是热闹非凡呢!安卓和鸿蒙系统两大巨头在游戏领域展开了一场激烈的较量。今...
安卓手机没有系统更,揭秘潜在风... 你有没有发现,现在安卓手机的品牌和型号真是五花八门,让人挑花了眼。不过,你知道吗?尽管市面上安卓手机...
充值宝带安卓系统,安卓系统下的... 你有没有发现,最近手机上的一款充值宝APP,在安卓系统上可是火得一塌糊涂呢!这不,今天就来给你好好扒...
安卓系统8.0镜像下载,轻松打... 你有没有想过,想要给你的安卓手机升级到最新的系统,却不知道从哪里下载那个神秘的安卓系统8.0镜像呢?...
安卓系统修改大全,全方位修改大... 你有没有想过,你的安卓手机其实是个大宝藏,里面藏着无数可以让你手机焕然一新的秘密?没错,今天就要来个...