当前位置:网站首页>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 !!!
边栏推荐
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- 【CVPR 2022】半监督目标检测:Dense Learning based Semi-Supervised Object Detection
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Penetration test --- database security: detailed explanation of SQL injection into database principle
- js导入excel&导出excel
- 每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
- 微信小程序uploadfile服务器,微信小程序之wx.uploadFile[通俗易懂]
- 刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
- Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
- DAY FIVE
猜你喜欢

DAY FIVE

Do you still have to rely on Simba to shout for a new business that is Kwai?

Introduction to GPIO

Gradle知識概括

Who said that new consumer brands collapsed? Someone behind me won

Today, I met a senior test developer from Tencent and saw the ceiling of the foundation

matplotlib画柱状图并添加数值到图中

DAY ONE

快讯 l Huobi Ventures与Genesis公链深入接洽中

leetcode:236. The nearest common ancestor of binary tree
随机推荐
【精品】pinia 基于插件pinia-plugin-persist的 持久化
GPIO簡介
Pytest multi process / multi thread execution test case
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
DAY TWO
Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
《LaTex》LaTex数学公式简介「建议收藏」
Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
How about the order management of okcc call center
零代码高回报,如何用40套模板,能满足工作中95%的报表需求
Unity color palette | color palette | stepless color change function
DAY THREE
陀螺仪的工作原理
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
DAY ONE
谁说新消费品牌大溃败?背后有人赢麻了
Cas d'essai fonctionnel universel de l'application
Random类的那些事
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work