天天看點

Linux第一個小程式-進度條\r&&\n 行緩沖區概念 倒計時程式進度條代碼

目錄

\r&&\n

行緩沖區概念

倒計時程式

進度條代碼

\r&&\n

回車概念 換行概念
  • \n
[[email protected] lesson8]# touch test.c
[[email protected] lesson8]# touoch Makefile
bash: touoch: command not found
[[email protected] lesson8]# touch Makefile
[[email protected] lesson8]# vim Makefile
[[email protected] 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
[[email protected] lesson8]# vim test.c
[[email protected] lesson8]# make
gcc -o mytest test.c
[[email protected] lesson8]# ls
Makefile  mytest  test.c
[[email protected] lesson8]# ./mytest
hello world
[[email protected] lesson8]# cat test.c
#include<stdio.h>
int main()
{
  printf("hello world\n");

  return 0;
}
           
  • \r
[[email protected] lesson8]# vim test.c
[[email protected] lesson8]# make
gcc -o mytest test.c
[[email protected] lesson8]# cat test.c
#include<stdio.h>
int main()
{
  printf("hello world\r");
 
  return 0;
}
[[email protected] lesson8]# ./mytest
[[email protected] lesson8]# make clean
rm -f mytest
           
  • \r\n
    [[email protected] lesson8]# vim test.c
    [[email protected] lesson8]# make clean
    rm -f mytest
    [[email protected] lesson8]# cat test.c
    #include<stdio.h>
    int main()
    {
      printf("hello world\r\n");
     
      return 0;
    }
    [[email protected] lesson8]# make 
    gcc -o mytest test.c
    [[email protected] 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接着列印[r[email protected] 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;
}
           
Linux第一個小程式-進度條\r&amp;&amp;\n 行緩沖區概念 倒計時程式進度條代碼
         我們嘗試用\r,其字元在緩沖區存放,并未重新整理,是以并不顯示
#include<stdio.h>
int main()
{
    int i=9;
    for(;i>=0;i--)
    {
         printf("%d\r",i);                                                                                             
         sleep(1);
    
    }
    
   return 0;
}
           
Linux第一個小程式-進度條\r&amp;&amp;\n 行緩沖區概念 倒計時程式進度條代碼
        我們使用函數對其重新整理,友善看倒計時
#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;
}
           

進度條代碼

樣式
Linux第一個小程式-進度條\r&amp;&amp;\n 行緩沖區概念 倒計時程式進度條代碼
建立
[[email protected] lesson8]# mkdir proc
[[email protected] lesson8]# cd proc
[[email protected] proc]# touch proc.c
[[email protected] proc]# touch proc.h
[[email protected] proc]# touch main.c
           
main.c内
#include "proc.h"
 
 int main()
 {
   process();
 
   return 0;
 }   
           
 proc.h内
[[email protected] proc]# vim proc.h
[[email protected] 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
[[email protected] proc]# touch Makefile
[[email protected] proc]# vim Makefile
[[email protected] proc]# cat Makefile
myprocess:main.c proc.c
	gcc -o myprocess main.c proc.c

.PHONY:clean
clean:
	rm -f myprocess