当前位置:网站首页>阿里大哥教你如何正确认识关于标准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对数据操作时要注意对缓冲区的问题
边栏推荐
- 大地坐标系转换火星坐标系
- ANSA二次开发 - Apps和ANSA插件管理
- 500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
- 【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
- HDU1847解题思路
- Pop up layer prompt in the background
- MySQL view event status statements and modification methods
- 有趣的 Kotlin 0x06:List minus list
- 关于MIT6.828_HW9_barriers xv6 homework9的一些问题
- El input limit can only input the specified number
猜你喜欢

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

LeetCode-学会复杂带随机指针链表的题(详解)

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

Leetcode topic

Im im development optimization improves connection success rate, speed, etc

小程序:scroll-view默认滑倒最下面

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

遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅

LeetCode-学会对无序链表进行插入排序(详解)

Ansa secondary development - build ansa secondary development environment on Visual Studio code
随机推荐
PHP calculate coordinate distance
ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
Rosen's QT journey 101 models and views in QT quick
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
redis源码优化--绑核
Multiple commands produce '... /xxx.app/assets.car' problem
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
Detailed record of steps to configure web server (many references)
ANSA二次开发 - 抽中面的两种方法
Analysis of echo service model in the first six chapters of unp
Hdu1847 problem solving ideas
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Reset grafana login password to default password
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
USB产品(FX3、CCG3PA)的调试方法
The local area network cannot access the Apache server
HM secondary development - data names and its use
egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
2021-04-02