当前位置:网站首页>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 j.journey 0-1 shortest path
- 小程序:获取元素节点信息
- redis源码优化--绑核
- MySQL view event status statements and modification methods
- CRC16 data verification supports modelbus and XMODEM verification modes (C language)
- Fx3 development board and schematic diagram
- UNP前六章 回射服务模型 解析
- “蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
- About mit6.828_ HW9_ Some problems of barriers xv6 homework9
- PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
猜你喜欢

egg(十九):使用egg-redis性能优化,缓存数据提升响应效率

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

Introduction and implementation of stack (detailed explanation)

nowcode-学会删除链表中重复元素两题(详解)

Ansa secondary development - apps and ansa plug-in management

阿里大哥教你如何正确认识关于标准IO缓冲区的问题

Sort 5-count sort

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

一小时内学会Abaqus脚本编程秘籍

Learn ABAQUS script programming script in an hour
随机推荐
How to set ticdc synchronization data to only synchronize the specified library?
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
Learn ABAQUS script programming script in an hour
HyperMesh自动保存(增强版)插件使用说明
About the web docking pin printer, lodop uses
MySQL view event status statements and modification methods
大地坐标系转换火星坐标系
PHP 图片上传
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
在vs code上配置Hypermesh二次开发环境
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
获取时间戳的三种方法的效率比较
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Installation of QT learning
Sort 4-heap sort and massive TOPK problem
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Ansa secondary development - build ansa secondary development environment on Visual Studio code
后台弹出layer提示
日常开发方案设计指北
Qt学习第一天