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

简介: 1.解释一下什么是缓冲区:2.缓冲区作用3.缓冲区的4种刷新策略:4.对比 ‘\n’ '\r'5.进度条的实现

目录


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'

1 2 3 4 \n

会在这里继续往下写

1 2 3 4 \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



目录
相关文章
|
1月前
|
小程序 Linux 开发工具
【Linux】Linux 开发工具(vim、gcc/g++、make/Makefile)+【小程序:进度条】-- 详解
【Linux】Linux 开发工具(vim、gcc/g++、make/Makefile)+【小程序:进度条】-- 详解
|
15天前
|
小程序 Linux C语言
Linux小程序 —— 进度条
Linux小程序 —— 进度条
22 6
|
4天前
|
Linux
【linux】重定向|缓冲区
【linux】重定向|缓冲区
6 0
|
4天前
|
Linux
【make/Makefile】Linux下进度条的设计与实现
【make/Makefile】Linux下进度条的设计与实现
|
19天前
|
Linux API C语言
C语言读写BMP文件-EasyBmp【 linux 平台】
**EasyBmp** 是一个49KB的轻量级C++图像处理库,专注于BMP格式,提供简单易用的API。它的特点是小巧、开源、易于理解和高度定制。通过示例代码展示了如何轻松读取、缩放和保存BMP图像。适合需要高效处理BMP图像的开发者。
|
23天前
|
JavaScript Java 测试技术
基于ssm+vue.js+uniapp小程序的C语言在线评测系统附带文章和源代码部署视频讲解等
基于ssm+vue.js+uniapp小程序的C语言在线评测系统附带文章和源代码部署视频讲解等
11 0
|
1月前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
38 5
|
1月前
|
缓存 Linux Shell
[Linux打怪升级之路]-缓冲区
[Linux打怪升级之路]-缓冲区
|
1月前
|
存储 Shell Linux
操作系统实战(一)(linux+C语言)
本篇文章重点在于利用linux系统的完成操作系统的实验,巩固课堂知识