当前位置:网站首页>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 !!!
边栏推荐
- [system analyst's road] Chapter 7 double disk system design (service-oriented development method)
- What is a responsive object? How to create a responsive object?
- Gradle knowledge generalization
- PostgreSQL uses pgpool II to realize read-write separation + load balancing
- [2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
- Server SMP, NUMA, MPP system learning notes.
- What is AVL tree?
- MVC and MVVM
- kubernetes部署ldap
- Basic chart interpretation of "Oriental selection" hot out of circle data
猜你喜欢
DAY SIX
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
App general function test cases
【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
什么是响应式对象?响应式对象的创建过程?
陀螺仪的工作原理
DAY FIVE
服务器SMP、NUMA、MPP体系学习笔记。
Basic chart interpretation of "Oriental selection" hot out of circle data
随机推荐
matplotlib画柱状图并添加数值到图中
TypeScript中使用类型别名
pinia 模块划分
Scholar doctor hahaha
Use type aliases in typescript
MVC and MVVM
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
Server SMP, NUMA, MPP system learning notes.
Gradle knowledge generalization
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Who said that new consumer brands collapsed? Someone behind me won
Matplotlib draws a histogram and adds values to the graph
How about the order management of okcc call center
DAY FOUR
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
Wechat applet UploadFile server, wechat applet wx Uploadfile[easy to understand]
[vector retrieval research series] product introduction
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
DAY ONE