当前位置:网站首页>阿里大哥教你如何正确认识关于标准IO缓冲区的问题
阿里大哥教你如何正确认识关于标准IO缓冲区的问题
2022-07-28 15:53:00 【InfoQ】
按照标准IO缓冲区可以分为三类:
不缓存类型:
- 一旦有数据,直接将数据写入到文件
行缓冲类型:
- 同全缓冲类型
- 遇到\n时,将数据写入文件
全缓冲类型:
- 当程序结束,将数据冲洗到文件
- 当遇见fflush(),将数据冲洗到文件
- 当文件关闭时,将数据冲洗到文件
- 当遇到读取操作,将数据冲洗到文件
- 当改变缓冲区的类型时,将数据冲洗到文件
- 当缓冲区满了,将数据冲洗到文件
#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ // w+ 以读写的方式打开文件,若文件存在则清空文件数据,若文件不存在,则创建文件 FILE *fp = fopen("_a.txt", "w+"); // 将数据写入文件 fputs("123", fp); //暂停程序不让,不让程序正常结束,观察a.txt文件内容 pause(); return 0;}#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("_a.txt", "w+"); fputs("123", fp); fflush(fp); pause(); return 0;}#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("_a.txt", "w+"); fputs("123", fp); fclose(fp); pause(); return 0;}#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("_a.txt", "w+"); fputs("123", fp); fgetc(fp); pause(); return 0;}
#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("_a.txt", "w+"); // setvbuf(fp, NULL, _IONBF, 0); fputs("123", fp); setvbuf(fp, NULL, _IONBF, 0); pause(); return 0;}#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ FILE *fp = fopen("_a.txt", "w+"); char buf[1024]; setvbuf(fp, buf, _IOLBF, 1024); //这里是行缓冲,如果要将数据冲洗至普通文件,注意要加\n fputs("123\n", fp); pause(); return 0;}总之,滞留在缓冲区中的数据有时被称为脏数据(dirty data),脏数据的存在代表程序操作的结果与文件真实状态不一致,若未正常冲洗这些脏数据就退出程序则有可能会造成数据丢失,我们在使用IO对数据操作时要注意对缓冲区的问题
边栏推荐
- 局域网无法访问apache服务器
- Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
- Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
- ANSYS secondary development - MFC interface calls ADPL file
- 微软100题-天天做-第11题
- Ansa secondary development - apps and ansa plug-in management
- PHP计算坐标距离
- Introduction and implementation of stack (detailed explanation)
- Qt学习之Qt Designer(设计师)
- 在vs code上配置Hypermesh二次开发环境
猜你喜欢

Introduction and implementation of queue (detailed explanation)

Introduction and implementation of stack (detailed explanation)

Two of C language programming!! Role of
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

HM secondary development - data names and its use

有趣的 Kotlin 0x09:Extensions are resolved statically

Sort 5-count sort

About standard IO buffers

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

小程序:scroll-view默认滑倒最下面
随机推荐
Li Hongyi, machine learning 4. Deep learning
2021-04-02
Sort 4-heap sort and massive TOPK problem
MySQL view event status statements and modification methods
Ansa secondary development - two methods of drawing the middle surface
HM二次开发 - Data Names及其使用
信号屏蔽与处理
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
Solve the width overflow of rich text pictures such as uniapp
排序4-堆排序与海量TopK问题
MySQL5.7及SQLyogV12安装及使用破解及常用命令
Qt学习之信号和槽机制
有趣的 Kotlin 0x09:Extensions are resolved statically
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
HyperMesh自动保存(增强版)插件使用说明
IM即时通讯软件开发网络请求成功率的优化
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
在vs code上配置Hypermesh二次开发环境
Curl returns blank or null without output. Solve the problem
CRC16 data verification supports modelbus and XMODEM verification modes (C language)