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

预备知识——缓冲区与换行

        先把重要结论放这:

  1. 数据的输入输出不会马上到达目的地,会先呆在缓冲区。
  2. 缓冲区相当于一行数据空间。
  3. 回车时,光标回到开头,相当于清空原有数据。
  4. 换行时,会直接将缓冲区数据输出。
  5. 程序结束时,会强制将缓冲区的数据输出。

        详情见我的另一篇文章:

缓冲区与回车换行-CSDN博客

写一个倒计时程序

#include<stdio.h>
#include<unistd.h>

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

 %-2d,保留两位数且向左对齐

编写入门版进度条

#include<stdio.h>
#include<unistd.h>
#include<string.h>

#define MAX 101
#define LABEL '='

int main()
{
  char bar[MAX];
  memset(bar, '\0', sizeof(bar));
  int count = 101;
  while(count--)
  {
    bar[100 - count] = LABEL;
    usleep(100000);
    printf("%s\r", bar);
    fflush(stdout);
  }
  printf("\n");
  return 0;
}

多文件编写升级版进度条

Version1

main.c

#include"processbar.h"

int main()
{
  processbar();
  return 0;
}

processbar.h

#pragma once

#include<stdio.h>

#define NUM 101
#define Body '='
#define Head '>'

void processbar();

processbar.c

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

const char *loading = "|/-\\";

void processbar()
{
  char bar[NUM];
  int n = strlen(loading);
  memset(bar, '\0', sizeof(bar));
  int cnt = 0;
  while(cnt <= 100)
  {
    bar[cnt++] = Body;
    if(cnt < 100)
      bar[cnt] = Head;
    printf("[%-101s][%3d%%][%c]\r", bar, cnt - 1, loading[cnt%n]);
    fflush(stdout);
    usleep(100000);
  }
  printf("\n");
}

makefile

processbar:main.o  processbar.o
	gcc -o $@ $^

main.o:main.c
	gcc -c main.c

processbar.o:processbar.c
	gcc -c processbar.c

.PHONY:clean

clean:
	rm processbar main.o processbar.o

Version2

main.c

#include"processbar.h"

int main()
{
  processbar();

  return 0;
}

processbar.h

#pragma once

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

#define NUM 101
#define Body '='
#define Head '>'
#define FILESIZE 1024*1024*1024

void processbar();

processbar.c

#include"processbar.h"

char bar[NUM];
const char *label = "|/-\\";

void processbar()
{
  int total = FILESIZE;
  int num = 0;
  srand(time(NULL)^1023);
  memset(bar, '\0', sizeof(bar));
  while(total)
  {
    usleep(100000);
    int one = rand()%(1024*1024*50);
    total -= one;
    if(total < 0) total = 0;
    double rate = ((FILESIZE - total)*1.0 / (FILESIZE)) * 100.0;
    num++; num %= 4;
    memset(bar, Body, sizeof(char)*((int)rate + 1));
    if((int)rate < 100) bar[(int)rate] = Head;
    printf("[%-101s][%6.2f%%][%c]\r", bar, rate, label[num]);
    fflush(stdout);
  }
  printf("\n");
}

makefile

processbar:main.o  processbar.o
	gcc -o $@ $^

main.o:main.c
	gcc -c main.c

processbar.o:processbar.c
	gcc -c processbar.c

.PHONY:clean

clean:
	rm processbar main.o processbar.o

相关推荐

  1. Linux第一程序---进度

    2024-06-07 14:14:01       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 14:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 14:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 14:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 14:14:01       20 阅读

热门阅读

  1. C#知识|封装典型的SQLServer数据库查询方法。

    2024-06-07 14:14:01       8 阅读
  2. Webpack基本配置

    2024-06-07 14:14:01       7 阅读
  3. 【运维】如何停止某个端口相关的所有服务

    2024-06-07 14:14:01       6 阅读
  4. ES6中如何使用class和extends关键字实现继承?

    2024-06-07 14:14:01       8 阅读
  5. 深耕低代码,技术赋能企业转型业务

    2024-06-07 14:14:01       8 阅读
  6. MySql什么时候表锁or行锁?

    2024-06-07 14:14:01       9 阅读
  7. 聊一下天,分享一下阿赵写技术博客的原因

    2024-06-07 14:14:01       13 阅读
  8. 嵌入式学习——网络编程(TCP)——day31

    2024-06-07 14:14:01       8 阅读
  9. screenrecord如何录屏

    2024-06-07 14:14:01       9 阅读