Linux 中好玩的小程序---缓冲区解释+进度条显示详解(c语言)

目录

1.解释一下什么是缓冲区:

2.缓冲区作用

3.缓冲区的4种刷新策略:

4.对比  ‘\n’  '\r'

5.进度条的实现


 

先来看一下效果:

CentOS 7 64 位 VMware 17

1.解释一下什么是缓冲区:

缓冲区简单来说是内存空间的一部分。也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的空间就叫做缓冲区。

2.缓冲区作用

简单可记为使低速的输入输出设备和高速的CPU能够协调工作,避免低速的输入输出设备占用CPU,解放出CPU,使其能够高效率工作,大大加快运行速度。

3.缓冲区的4种刷新策略:

a. 无缓冲

可理解为立即执行,不进行刷新,标准出错情况的典型代表stderr,这使得出错信息可以马上直接地显示出来。

b. 行缓冲

只有在输入或者是输出中遇到换行符(\n)的时候才会进行刷新操作。

c. 全缓冲

只有当缓冲区满了的时候才会进行刷新。典型代表磁盘文件的读写。

d. 程序退出会自动刷新。

4.对比  ‘\n’  '\r'

1234\n 
    会在这里继续往下写 
      
1234\r 
会在这里继续往下写     
      

5.进度条的实现

 首先先看代码:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define max 101
  4 void test()
  5 {
  6 
  7 
  8     int i=0;
  9     const char* lable="|/-\\";
 10     char nums[max];
 11     memset(nums,'\0',sizeof(nums));
 12     while(i<=100)
 13     {
 14        printf("[%-100s][%-3d%%][%c]\r",nums,i,lable[i%4]);
 15         fflush(stdout);
 16         nums[i++]='#';
 17         usleep(30000);
 18 }
 19 printf("\n");
 20 }
 21 int main()
 22 {
 23    test();
 24     return 0;
 25 }

注释:

1. max定义为101,是为了预留出 字符‘\0’,字符串输出遇‘\0’结束。

2. const char* lable="|/-\\"; 

用两个反斜杠, 因为c语言语法问题,这里模拟的是光标旋转的图像,可以用小时候所看的连环画来理解。

3. memset(nums,'\0',sizeof(nums));

这里是利用memset函数来给数组赋值,也可以直接写成char nums[max]={"\0"}; 效果是一样的。

4.  printf("[%-100s][%-3d%%][%c]\r",nums,i,lable[i%4]); 

%100s 、%3d即是格式化控制符,用来预留自己设定的空间大小。如果不加负号,在c语言中默认是右对齐的,进度条会从右往左走。

5. lable[i%4]

如果直接写 i ,随着i的增大,必定会造成越界访问问题。用 i%4 可很好的解决这个问题。

6. usleep单位是微秒(千分之一毫秒),sleep单位是秒

7.  fflush(stdout);

用来强制刷新,不用缓冲直接输出在显示器上。

以下是 fflush 的详细介绍:

NAME
       fflush - flush a stream

SYNOPSIS
       #include <stdio.h>

       int fflush(FILE *stream);

DESCRIPTION
       For  output  streams,  fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function.  For input streams, fflush() discards
       any buffered data that has been fetched from the underlying file, but has not been consumed by the application.  The open status of the stream is unaffected.

       If the stream argument is NULL, fflush() flushes all open output streams.

       For a nonlocking counterpart, see unlocked_stdio(3).

RETURN VALUE
       Upon successful completion 0 is returned.  Otherwise, EOF is returned and errno is set to indicate the error.

ERRORS
       EBADF  Stream is not an open stream, or is not open for writing.

       The function fflush() may also fail and set errno for any of the errors specified for write(2).

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌──────────┬───────────────┬─────────┐
       │Interface │ Attribute     │ Value   │
       ├──────────┼───────────────┼─────────┤
       │fflush()  │ Thread safety │ MT-Safe │
       └──────────┴───────────────┴─────────┘
CONFORMING TO
       C89, C99, POSIX.1-2001, POSIX.1-2008.

       The standards do not specify the behavior for input streams.  Most other implementations behave the same as Linux.

NOTES
       Note that fflush() only flushes the user-space buffers provided by the C library.  To ensure that the data is physically stored on disk the kernel buffers must be  flushed  too,  for  example,  with
       sync(2) or fsync(2).

SEE ALSO
       fsync(2), sync(2), write(2), fclose(3), fopen(3), setbuf(3), unlocked_stdio(3)

COLOPHON

 

 

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洁洁!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值