【Linux系统编程】IO标准缓冲区
2020-03-06 10:48
260 查看
00. 目录
文章目录
01. 缓冲区概述
标准I/O提供了三种类型的缓冲:
1、全缓冲:
在填满标准I/O缓冲区后才进行实际I/O操作。常规文件(如普通文本文件)通常是全缓冲的。
2、行缓冲:
当在输入和输出中遇到换行符时,标准I/O库执行I/O操作。这允许我们一次输出一个字符,但只有在写了一行之后才进行实际I/O操作。标准输入和标准输出对应终端设备(如屏幕)时通常是行缓冲的。
3、不带缓冲:
用户程序每次调库函数做写操作都要通过系统调用写回内核(如系统调用函数)。标准错误输出通常是无缓冲的,这样用户程序产生的错误信息可以尽快输出到设备。
02. 全缓冲
测试代码:
int main(int argc, char *argv[]) { FILE *fp = NULL; // 读写方式打开,文件不存在则创建 fp = fopen("test.txt", "w+"); if(NULL == fp) { printf("open error\n"); return 1; } char *str = "C++程序员\n"; fwrite(str, 1, strlen(str), fp); // 往文件写内容 while(1); // 程序阻塞在这里 return 0; }
运行程序发现,test.txt并没有内容。因为常规文件通常是全缓冲的,只有缓冲区满了后,才会把内容写到文件中。接下来,我们改一下上面那个例子。
测试代码:
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { FILE *fp = NULL; // 读写方式打开,文件不存在则创建 fp = fopen("test.txt", "w+"); if(NULL == fp) { printf("open error\n"); return 1; } char *str = "test\n"; int i = 0; while(i <= 512){ // 缓冲区大小不确定,i的大小只是一个调试值 fwrite(str, 1, strlen(str), fp); // 往文件写内容 i++; } while(1); // 程序阻塞在这里 return 0; }
上面的例子是循环给文件写内容,让缓冲区有填满的可能,结果发现,文件是有内容的。实际上要想成功给文件写进内容,除了缓冲区填满,还有别的方法。
2.1 人为关闭文件,就算缓冲区没有填满,内容也会写进文件
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { FILE *fp = NULL; // 读写方式打开,文件不存在则创建 fp = fopen("test.txt", "w+"); if(NULL == fp) { printf("open error\n"); return 1; } char *str = "test ok\n"; fwrite(str, 1, strlen(str), fp); // 往文件写内容 fclose(fp); // 人为关闭文件,就算缓冲区没有填满,内容也会写进文件 while(1); // 程序阻塞在这里 return 0; }
2.2 程序正常结束,就算缓冲区没有填满,没有关闭文件,内容也会写进文件。
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { FILE *fp = NULL; // 读写方式打开,文件不存在则创建 fp = fopen("test.txt", "w+"); if(NULL == fp) { printf("open error\n"); return 1; } char *str = "test ok\n"; fwrite(str, 1, strlen(str), fp); // 往文件写内容 return 0; // 程序正常结束,就算缓冲区没有填满,没有关闭文件,内容也会写进文件。 }
03. 行缓冲
#include <stdio.h> int main(int argc, char *argv[]) { printf("hello test"); while(1); return 0; }
运行这个程序,会发现 hello test并没有打印到屏幕上。因为标准输入和标准输出对应终端设备时通常是行缓冲的,当在输入和输出中遇到换行符时,标准I/O库执行I/O操作。如下:
#include <stdio.h> int main(int argc, char *argv[]) { printf("hello test\n"); while(1); return 0; }
除了遇到换行符,还有别的方法可以执行I/O操作。
3.1 缓冲区满
int main(int argc, char *argv[]) { while(1) { // 循环打印,总有缓冲区填满的可能 printf("hello sunplusedu"); } while(1); return 0; }
3.2 人为刷新缓冲区
#include <stdio.h> int main(int argc, char *argv[]) { printf("hello test"); fflush(stdout); // 人为刷新 while(1); return 0; }
3.3 程序正常结束
#include <stdio.h> int main(int argc, char *argv[]) { printf("hello sunplusedu"); return 0; // 程序正常结束 }
04. 不带缓冲区
#include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { char *str = "hello test"; // 有没有\n,缓冲区有没有填满,都没关系 write(1, str, strlen(str)); // 往标准输出写内容 while(1); return 0; }
Linux系统调用中的IO函数一般不带有缓冲区。
05. 附录
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- Linux系统编程:标准IO操作--索引对应路径下的文件
- Linux系统编程:标准IO操作--索引对应路径下的文件
- Linux系统编程_4_标准I/O(附:清空缓冲区方法)
- 二、linux IO 编程---系统调用和POSIX标准和标准IO
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
- linux系统编程之基础必备(二):C 标准IO 库函数与Unbuffered IO函数
- linux系统编程之文件与IO(八):文件描述符相关操作-dup,dup2,fcntl
- linux系统编程之文件与io(五)
- linux c/c++ 学习总结(7)-- 标准库中IO缓冲区
- Linux IO系统编程系列课程笔记之文件原子操作
- 嵌入式Linux标准IO,time(),localtime(),sleep(),fprintf(),每隔1秒向文件test.txt中写如当前系统时间
- Linux系统编程:简单文件IO操作
- Linux IO 系统编程系列课程笔记之fcntl函数
- Linux编程基础:C标准库IO缓冲区与内核缓冲区的区别
- 标准IO与linux系统IO的缓冲问题
- yuchuan_Linux_C 编程之七系统IO函数
- Linux下C编程,进程通信之标准流管道通信(即系统调用)
- Linux系统编程与网络编程——文件IO介绍(一)
- Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)
- linux系统编程之文件与io(四)