当前位置:网站首页>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 !!!
边栏推荐
- GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
- How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
- What is a responsive object? How to create a responsive object?
- Gradle knowledge generalization
- 1000 words selected - interface test basis
- AVL树到底是什么?
- Design a red envelope grabbing system
- How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
- app通用功能测试用例
- openresty ngx_lua子请求
猜你喜欢
2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
Server SMP, NUMA, MPP system learning notes.
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
leetcode:236. The nearest common ancestor of binary tree
DAY FIVE
什么是响应式对象?响应式对象的创建过程?
Cas d'essai fonctionnel universel de l'application
Yaduo Sangu IPO
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
随机推荐
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
2022年PMP项目管理考试敏捷知识点(9)
谁说新消费品牌大溃败?背后有人赢麻了
基于SSM框架实现的房屋租赁管理系统
DAY SIX
What is AVL tree?
三句话简要介绍子网掩码
Supersocket 1.6 creates a simple socket server with message length in the header
Yaduo Sangu IPO
为什么完全背包要用顺序遍历?简要解释一下
Scholar doctor hahaha
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
Typescript incremental compilation
TypeScript增量编译
在Docker中分分钟拥有Oracle EMCC 13.5环境
Use Yum or up2date to install the postgresql13.3 database
web渗透测试是什么_渗透实战
(leetcode) sum of two numbers
快讯 l Huobi Ventures与Genesis公链深入接洽中
Server SMP, NUMA, MPP system learning notes.