Linux第一个小程序-进度条

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

\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<stdio.h>
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<stdio.h>
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<stdio.h>
    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 <stdio.h>
int main()
{
 printf("hello world\n");
 sleep(3);
 return 0;
}

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

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

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

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

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

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

#include <stdio.h>
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<stdio.h>
int main()
{
  printf("hello world\r");
  sleep(3);
  fflush(stdout);
  return 0;
}

修改后

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

倒计时程序

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

         

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

#include<stdio.h>
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<stdio.h>
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 <stdio.h>

​extern void process();

proc.c内

#include "proc.h"
#include<string.h>
#include<unistd.h>

#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.c
	gcc -o myprocess main.c proc.c

.PHONY:clean
clean:
	rm -f myprocess

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: linux

“Linux第一个小程序-进度条” 的相关文章