当前位置:网站首页>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
边栏推荐
- 小程序:获取元素节点信息
- Ansa secondary development - Introduction to interface development tools
- Qt学习第一天
- How to set ticdc synchronization data to only synchronize the specified library?
- MD5加密验证
- First day of QT study
- 优化Hypermesh脚本性能的几点建议
- Hdu1847 problem solving ideas
- 关于 CMS 垃圾回收器,你真的懂了吗?
- Interesting kotlin 0x07:composition
猜你喜欢

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult

排序2-冒泡排序与快速排序(递归加非递归讲解)

QT packaging

关于web对接针式打印机问题,Lodop使用

Interesting kotlin 0x09:extensions are resolved statically

ANSA二次开发 - 抽中面的两种方法

Leetcode learn complex questions with random pointer linked lists (detailed explanation)

LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数

Leetcode daily practice - 160. Cross linked list

在abaqus中使用PyQt设计GUI
随机推荐
Sort 5-count sort
PHP gets the applet code, and the applet jumps with parameters
Curl returns blank or null without output. Solve the problem
重置grafana登录密码为默认密码
关于 CMS 垃圾回收器,你真的懂了吗?
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
配置web服务器步骤详细记录(多有借鉴)
PHP图片合成技术
HM secondary development - data names and its use
ANSYS二次开发 - MFC界面调用ADPL文件
Im im development optimization improves connection success rate, speed, etc
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
Introduction and implementation of queue (detailed explanation)
Solve the width overflow of rich text pictures such as uniapp
Multiple commands produce '... /xxx.app/assets.car' problem
Qt学习之信号和槽机制
有趣的 Kotlin 0x08:What am I
WSL+Valgrind+Clion
PHP获取小程序码,小程序带参数跳转