标准IO缓冲机制
参考资料:
https://q16964777.iteye.com/blog/2228244
知道缓冲有几种模式:无缓冲、行缓冲、全缓冲。通过判断FILTE中的 _flags 的判断可以知道究竟是那种缓冲模式。
#include <stdio.h>
int stream_attribute(FILE *fp)
{
if(fp->_flags & _IO_UNBUFFERED)
{
printf("The IO type is unbuffered\n");
}else if(fp->_flags & _IO_LINE_BUF){
printf("The IO type is line buf\n");
}else{
printf("The IO type is full buf\n");
}
printf("The IO size : %d\n",fp->_IO_buf_end - fp->_IO_buf_base);
return ;
}
int main()
{
FILE *fp;
stream_attribute(stdin);
printf("___________________________________\n\n");
stream_attribute(stdout);
printf("___________________________________\n\n");
stream_attribute(stderr);
printf("___________________________________\n\n");
if((fp = fopen("test.txt","w+")) == NULL)
{
perror("fail to fopen");
}
stream_attribute(fp);
return ;
} 我们修改一下代码再看
#include <stdio.h>
int stream_attribute(FILE *fp)
{
if(fp->_flags & _IO_UNBUFFERED)
{
printf("The IO type is unbuffered\n");
}else if(fp->_flags & _IO_LINE_BUF){
printf("The IO type is line buf\n");
}else{
printf("The IO type is full buf\n");
}
printf("The IO size : %d\n",fp->_IO_buf_end - fp->_IO_buf_base);
return ;
}
int main()
{
FILE *fp;
getchar();
stream_attribute(stdin);
printf("___________________________________\n\n");
stream_attribute(stdout);
printf("___________________________________\n\n");
stream_attribute(stderr);
printf("___________________________________\n\n");
if((fp = fopen("test.txt","w+")) == NULL)
{
perror("fail to fopen");
}
printf("before write:\n");
stream_attribute(fp);
fputc('a',fp);
printf("after write:\n");
stream_attribute(fp);
return ;
}
另外要清楚,缓冲区是在执行读写操作之后才分配的。
标准IO缓冲机制的更多相关文章
- linux标准IO缓冲(apue)
为什么需要标准IO缓冲? LINUX用缓冲的地方遍地可见,不管是硬件.内核还是应用程序,内核里有页高速缓冲,内存高速缓冲,硬件更不用说的L1,L2 cache,应用程序更是多的数不清,基本写的好的软件 ...
- 为什么需要标准IO缓冲?
(转)标准I/O缓冲:全缓冲.行缓冲.无缓冲 标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的 ...
- C++(四十六) — 异常处理机制、标准IO输入输出
1.异常处理机制 一般来说,异常处理就是在程序运行时对异常进行检测和控制.而在C++ 中,使用 try-throw-catch模式进行异常处理的机制. #include<iostream> ...
- 标准IO的缓冲问题
在看APU时,第8章进程时, #include <stdio.h> #include <unistd.h> ; char buf[] = "a write to st ...
- [Linux]标准IO全缓冲和行缓冲
概述 标准IO中,标准错误是不带缓冲的.若是指向终端设备的流才是行缓冲的,否则是全缓冲的. 行缓冲也可以分配缓冲区,当遇到超大行(超过缓冲区的行),缓冲区内容也会优先刷出. 示例 #include & ...
- 第十三篇:带缓冲的IO( 标准IO库 )
前言 在之前,学习了 read write 这样的不带缓冲IO函数. 而本文将讲解标准IO库中,带缓冲的IO函数. 为什么要有带缓冲IO函数 标准库提供的带缓冲IO函数是为了减少 read 和 wri ...
- 带缓冲的IO( 标准IO库 )
前言 在之前,学习了 read write 这样的不带缓冲IO函数.而本文将讲解标准IO库中,带缓冲的IO函数. 为什么要有带缓冲IO函数 标准库提供的带缓冲IO函数是为了减少 read 和 writ ...
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
最近学习linux系统下的应用编程,参考书籍是那本称为神书的<Unix环境高级编程>,个人感觉神书不是写给草鞋看的,而是 写给大神看的,如果没有一定的基础那么看这本书可能会感到有些头重脚轻 ...
- (九)errno和perror、标准IO
3.1.6.文件读写的一些细节3.1.6.1.errno和perror(1)errno就是error number,意思就是错误号码.linux系统中对各种常见错误做了个编号,当函数执行错误时,函数会 ...
随机推荐
- vue 学习笔记1
1.子组件在父组件的原生的事件例如一个child子组件的点击事件<child @click="handleClick">点击</child>这种情况在父组件 ...
- linux下WIFI的AP搜索、连接方法
wpa_supplicant -Dwext -ieth1 -c/etc/wpa_supplicant.conf &wpa_cli save_configwpa_cli reconfigure ...
- Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)
这是因为测试代码时遇到错误,它会停止编译.只需要在pom.xml的<project>里添加以下配置,使得测试出错不影响项目的编译.<build> <plugins> ...
- 在project窗口中快速定位文件
[在project窗口中快速定位文件] 点击带圆圈的小叉叉按钮,这个时候Project中就会定位到当前文件目录下了. 参考:http://blog.csdn.net/hyr83960944/artic ...
- spring boot 微服务例子一
package com.example.hello.demo; import org.springframework.boot.SpringApplication;import org.springf ...
- Python 多继承与MRO-C3算法
继承关系图:树结构 广度优先遍历:先找A,再找B.C,最后找D.E.(顺序:A.B.C) 深度优先遍历:先找A,再找B,接着找D.E(把B里面找完):然后找C.(顺序:A.B.D.E.C) MRO-C ...
- Mybatis学习链接
mybatis实战教程(mybatis in action),mybatis入门到精通 http://blog.csdn.net/techbirds_bao/article/details/923 ...
- tab template
<div class="box"> <div class="box-body"> <div class="nav-tab ...
- PyCon大会Python主题演讲摘要
PyCon 是全国际最大的以 Python 编程言语 为主题的技能大会.大会由 Python 社区组织,每年举行一次.在大会上,来自国际各地的 Python 用户与中心开发者齐聚一堂,共同同享 Pyt ...
- 在timer的时候突然改变影片简介,先前的不暂停
import flash.display.MovieClip; import flash.utils.Timer; import flash.events.TimerEvent; var hinder ...