当前位置:网站首页>Brother Ali teaches you how to correctly understand the problem of standard IO buffer
Brother Ali teaches you how to correctly understand the problem of standard IO buffer
2022-07-28 16:49:00 【InfoQ】
According to the standard IO Buffers can be divided into three categories :
Do not cache type :
- Once there's data , Write data directly to a file
Row buffer type :
- Same as full buffer type
- encounter \n when , Write data to file
Full buffer type :
- When the program ends , Flush data to file
- When I meet fflush(), Flush data to file
- When the file is closed , Flush data to file
- When encountering read operation , Flush data to file
- When changing the type of buffer , Flush data to file
- When the buffer is full , Flush data to file
#include <stdio.h>#include <unistd.h>int main(int argc, char const *argv[]){ // w+ Open the file read-write , If the file exists, empty the file data , If the file does not exist , Create file FILE *fp = fopen("_a.txt", "w+"); // Write data to file fputs("123", fp); // Pause the program and don't let , Don't let the program end normally , Observe a.txt The contents of the document 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); // Here is the line buffer , If you want to flush the data into ordinary files , Pay attention to add \n fputs("123\n", fp); pause(); return 0;}All in all , Data stuck in buffers is sometimes referred to as dirty data (dirty data), The existence of dirty data means that the result of program operation is inconsistent with the real state of the file , If you exit the program without flushing these dirty data normally, it may cause data loss , We are using IO Pay attention to the buffer when operating on data
边栏推荐
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
- Ansa secondary development - Introduction to interface development tools
- 关于 CMS 垃圾回收器,你真的懂了吗?
- Sort 5-count sort
- CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
- 阿里大哥教你如何正确认识关于标准IO缓冲区的问题
- 微软100题-天天做-第11题
- ANSA二次开发 - 界面开发工具介绍
- 遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
- 有趣的 Kotlin 0x0A:Fun with composition
猜你喜欢

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

Interesting kotlin 0x0a:fun with composition

HM secondary development - data names and its use

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

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

有趣的 Kotlin 0x0A:Fun with composition

关于 CMS 垃圾回收器,你真的懂了吗?

排序1-插入排序与希尔排序

HyperMesh自动保存(增强版)插件使用说明

排序5-计数排序
随机推荐
微信公众号获取素材列表
优化Hypermesh脚本性能的几点建议
有趣的 Kotlin 0x07:Composition
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Implementation of paging
排序2-冒泡排序与快速排序(递归加非递归讲解)
PHP 图片上传
Im im development optimization improves connection success rate, speed, etc
PHP gets the applet code, and the applet jumps with parameters
Leetcode daily practice - 160. Cross linked list
The local area network cannot access the Apache server
Redis系列4:高可用之Sentinel(哨兵模式)
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
Li Hongyi, machine learning 4. Deep learning
Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
LeetCode每日一练 —— 160. 相交链表
FX3开发板 及 原理图
Headline article_ signature