当前位置:网站首页>阿里大哥教你如何正确认识关于标准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对数据操作时要注意对缓冲区的问题
边栏推荐
- mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
- Sort 4-heap sort and massive TOPK problem
- Applet: get element node information
- Detailed record of steps to configure web server (many references)
- LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数
- Ansa secondary development - build ansa secondary development environment on Visual Studio code
- Fx3 development board and schematic diagram
- 排序1-插入排序与希尔排序
- 微软100题-天天做-第16题
- php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
猜你喜欢

Several methods of HyperMesh running script files

队列的介绍与实现(详解)

Configure HyperMesh secondary development environment on vs Code

QT packaging

Sort 4-heap sort and massive TOPK problem

The video Number finds the golden key, and Tiktok imitates the latecomers

Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)

微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能

WSL+Valgrind+Clion

About standard IO buffers
随机推荐
Qt学习之安装
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
Qt学习之信号和槽机制
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
2021-04-02
Ansa secondary development - two methods of drawing the middle surface
有趣的 Kotlin 0x07:Composition
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Rosen's QT journey 102 listmodel
Using pyqt to design gui in ABAQUS
排序5-计数排序
FX3开发板 及 原理图
日常开发方案设计指北
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
Microsoft question 100 - do it every day - question 11
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
PHP获取小程序码,小程序带参数跳转
Introduction and implementation of stack (detailed explanation)
WSL+Valgrind+Clion
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路