当前位置:网站首页>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 !!!
边栏推荐
- How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
- Server SMP, NUMA, MPP system learning notes.
- Oracle对表进行的常用修改命令
- openresty ngx_ Lua subrequest
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
- Use Yum or up2date to install the postgresql13.3 database
- Résumé des connaissances de gradle
- DAY FOUR
- App general function test cases
猜你喜欢

专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统

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

量子时代计算机怎么保证数据安全?美国公布四项备选加密算法

Pytest multi process / multi thread execution test case
![[automated testing framework] what you need to know about unittest](/img/4d/0f0e0a67ec41e41541e0a2b5ca46d9.png)
[automated testing framework] what you need to know about unittest

Matplotlib draws a histogram and adds values to the graph

Please help xampp to do sqlilab is a black

What is AVL tree?

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

Basic chart interpretation of "Oriental selection" hot out of circle data
随机推荐
PostgreSQL uses pgpool II to realize read-write separation + load balancing
Who said that new consumer brands collapsed? Someone behind me won
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
Supersocket 1.6 creates a simple socket server with message length in the header
Pinia module division
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
AVL树到底是什么?
PostgreSQL highly available repmgr (1 master 2 slave +1witness) + pgpool II realizes master-slave switching + read-write separation
Scholar doctor hahaha
1000 words selected - interface test basis
rancher集成ldap,实现统一账号登录
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
在Docker中分分钟拥有Oracle EMCC 13.5环境
MATLIB reads data from excel table and draws function image
[212] what are three methods for PHP to send post requests
kubernetes部署ldap
陀螺仪的工作原理
app通用功能測試用例
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
MIT 6.824 - Raft学生指南