当前位置:网站首页>C language input / output stream and file operation [II]
C language input / output stream and file operation [II]
2022-07-07 00:01:00 【jie3606】
File operations
FILE Structure and buffer are discussed in depth
FILE Introduction to structure
Due to different compiler pairs FILE The implementation scheme of structure type is different Here only vs 2012 stdio.h Medium FILE Structure as an example :
struct _iobuf{
char* _ptr;// Point to the location information of the buffer to read and write data
int _cnt;//"W": Size of remaining bytes in buffer ,“r”: The byte size of the data write buffer
char* _base;// Point to the base address of the buffer
int _flag;// Status flag
int _file;// file type
int _charbuf;//
int _bufsize;// Size of buffer
char* _tmpfname;// Temporary file name
};
typedef struct _iobuf FILE;
buffer
To be completed !!!
What's the difference between a text file and a binary file
The difference between text file and binary file
text file Files are usually used to store content that is visible to the naked eye , for example :.txt、.c、.dat Wait for the documents , We open these files through a text editor , You can understand the contents of the document .
Binary Usually used to save videos 、 picture 、 Procedures, etc , Open these files with a text editor , You will see a lot of garbled code .
But the essence of physical storage is the same , It's all binary code , The reason why we can understand the content of text files , Because... Is used in the text file ASCII、UTF-8、GBK Equal character coding , The text editor can recognize these encoding formats , And convert the encoded value into characters to display .
Binary files use mp4、gif、exe And other special coding formats , Text editors don't recognize these encoding formats , It can only be parsed randomly according to the character encoding format , So it's a mess of characters , Some haven't even seen .
in general , Documents are the carrier of information , We need to know its storage type and interpret it in the correct form to get useful information .
fopen() Text mode and binary mode in (Windows Systems and Linux Line breaks for text files )
There is no essential difference between text files and binary files , Only the handling of newline symbols is different .
C Language program will \n As a line break , class UNIX/Linux The system will also \n As a line break , So the data in the program will be written to the text file intact , vice versa .
however Windows The system is different , It will \r\n As a newline character in a text file .
stay Windows In the system , If you open a file as text , When reading a file , The program will all the in the file r\n Convert to one character \n. in other words , If two consecutive characters in a text file are \r\n, The program will discard the previous \r, Read in only \n.
When writing to a file , Program meeting take \n convert to \r\n write in . in other words , If there are characters in the content to be written \n, Before writing this character , The program will automatically write a \r.
therefore , If the binary file is opened in text mode for reading and writing , The contents read and written may be different from the contents of the document .
In total , about Windows platform , For the sake of insurance , We'd better use "t" To open a text file , use "b" To open binary files . about Linux platform , Use "t" still "b" It doesn't matter , Since the default is "t", Then let's just write nothing .
C Language implementation getFileSize
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
int main() {
FILE* fp = fopen("test.txt", "r");
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
rewind(fp);
fclose(fp);
printf(" The length of the file is :%d", len);// In file 0D 0A
return 0;
}
C Language implementation my_cp
stay C Language input / Output stream and file operation 【 One 】 The article gives my_cp But the efficiency of replication without consideration is low , Efficient file replication will be achieved here .
Realize file replication Main idea yes : Open a buffer and constantly read data from the file into the buffer , Whenever the data is read once, the data in the buffer will be written to a new file at one time , Until the contents of the source file are all read .
Two key issues :
- How big a buffer should be opened ?
- Buffer has no end flag , If the buffer is not full , How to determine the number of bytes written ?
- At present, most disk sectors are 4K The alignment of the , If the data written is not 4K An integer multiple of will be read across sectors , Reduce efficiency , So open up 4K The buffer .
- Each read returns the number of bytes read .
#include<stdio.h>
#include<stdlib.h>
int main(int arg ,char* argv[]){
if(arg!=3){
puts("input error");
exit(0);
}
FILE * fpr = fopen(argv[1],"rb");
if(fpr == NULL){
puts("file open error");
exit(0);
}
FILE * fpw = fopen(argv[2],"wb");
if(fpw == NULL ){
puts("file open error");
exit(0);
}
int readSize = 1024 * 4;
char* readData = (char*) malloc(readSize);
if(readData == NULL){
puts("malloc error");
exit(0);
}
int realReadSize;
while((realReadSize = fread(readData,1,readSize,fpr))>0){
fwrite(readData,realReadSize,1,fpw);
}
free(readData);
fclose(fpr);
fclose(fpw);
return 0;
}
// Linux Ubantu 20.4
// gcc -o my_cp main.c
// ./my_cp sorceFileName newFileName
C Language implementation file insertion 、 Delete 、 change
To be completed !!!
边栏推荐
- Penetration test --- database security: detailed explanation of SQL injection into database principle
- GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
- Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
- iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
- [unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
- Oracle EMCC 13.5 environment in docker every minute
- (LeetCode)两数之和
- Please help xampp to do sqlilab is a black
- 《LaTex》LaTex数学公式简介「建议收藏」
- STM32 enters and wakes up the stop mode through the serial port
猜你喜欢
Who said that new consumer brands collapsed? Someone behind me won
AVL树到底是什么?
[OFDM communication] OFDM system signal detection based on deep learning with matlab code
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
谁说新消费品牌大溃败?背后有人赢麻了
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
Penetration test --- database security: detailed explanation of SQL injection into database principle
从外企离开,我才知道什么叫尊重跟合规…
Rider离线使用Nuget包的方法
随机推荐
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
pinia 模块划分
1000字精选 —— 接口测试基础
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
MVC and MVVM
Matplotlib draws a histogram and adds values to the graph
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
基于SSM框架实现的房屋租赁管理系统
【212】php发送post请求有哪三种方法
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
DAY FOUR
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
PostgreSQL uses pgpool II to realize read-write separation + load balancing
Design of short chain
The largest single investment in the history of Dachen was IPO today
How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
openresty ngx_lua子请求
GPIO簡介