当前位置:网站首页>Document operation II (5000 word summary)
Document operation II (5000 word summary)
2022-04-23 16:57:00 【Populus euphratica under the desert】
Personal home page : Welcome to ——> Populus euphratica under the desert
Ladies and gentlemen , It's beautiful
If you think the article is helpful to you
You can support bloggers with one key and three links
Your every care is the driving force of my persistence
: This issue focuses on : Explanation of knowledge related to document operation
The first part mainly talks about the sequential reading and writing of documents , Little friends who don't understand this point ——》 file 1
I hope everyone can study and work happily every day .
Let's move on to today's study :
So how to read and write files randomly ?
So how to calculate the offset ?
How to make the file pointer point to the starting position ?
The difference between binary files and text files
Judgment of the end of file reading
Whether there is the concept of buffer :
printf and fprintf function sprintf
So how to read and write files randomly ?
Make the same fseek Function for random reading and writing , Let's look at the function prototype for analysis :
First , The first function parameter is the file stream , Nothing to say.
The second parameter is the offset , The offset has a relative position , Offset from file pointer position .
The third parameter is the file pointer position , There are three states
SEEK_CUR
The current location of the file pointer
SEEK_END
End of file pointer
SEEK_SET
File pointer start position
in summary : We have to read and write randomly , It's at the position of the file pointer , Adding the offset can realize the random reading and writing of the file .
example :
int main() { FILE *p = fopen(" Populus euphratica .dat", "wb"); if (p == NULL) { perror("main:"); exit(-1); } fputs("Happy life", p); fseek(p, 6, SEEK_SET); fputs("every day", p); fclose(p); return 0; }
First write “Happy life”, Then we start at the beginning of the file pointer , The offset for the 6, To write , It will cover “Happy ” After that , It becomes "Happy every day".
Random reading of files :
int main() { FILE *p = fopen(" Populus euphratica .dat", "rb"); if (p == NULL) { perror("main:"); exit(-1); } char a[10] = { 0 }; fseek(p, 4, SEEK_SET); fgets(a, 10, p); fclose(p); return 0; }
First, the file pointer is the starting position of the file , The offset for the 4, So, from ’y‘ Characters begin to read , Read back 9 Characters , because fgets Finally, I will read a '\0'.
So how to calculate the offset ?
We use it ftell Function to confirm the offset of the current position from the starting position .
The function prototype is
Just pass in the file pointer of the current location .
Look at an example :
int main() { FILE *p = fopen(" Populus euphratica .dat", "rb"); if (p == NULL) { perror("main:"); exit(-1); } fseek(p, -3, SEEK_END); printf("%c",fgetc(p)); int a = ftell(p); printf("\n%d\n", a); fclose(p); return 0; }
Under the analysis of : First, we find the end of the file pointer , Then the offset is -3 , Is to offset forward from the end of the file 3 Characters , Then read is d('a' 'y' ’\0‘), At this time, the file pointer is located at 'a' It's about , The distance from the start of the file is 13 The offset .
How to make the file pointer point to the starting position ?
We can use rewind function To achieve
int main() { FILE *p = fopen(" Populus euphratica .dat", "rb"); if (p == NULL) { perror("main:"); exit(-1); } fseek(p, -3, SEEK_END); printf("%c",fgetc(p)); int a = ftell(p); printf("\n%d\n", a); rewind(p); a = ftell(p); printf("%d\n", a); fclose(p); return 0; }
Use rewind Then the offset of the file is 0 La .
The difference between binary files and text files
According to the organization of data , Data files are called text file perhaps Binary .Data is stored in memory in binary form , If the output without conversion is to external memory , Binary files . .
With ASCII A file stored in the form of characters is a text file .All characters are written in ASCII stored , Numerical data can be used either ASCII stored , It can also be stored in binary form .Let's take an example to understand :int main() { int a = 10000; FILE* pf = fopen(" Under the tree .txt", "wb"); fwrite(&a, 4, 1, pf); fclose(pf); pf = NULL; return 0; }
We put 10000 Write to the file in binary , Let's have a look , First use Text form Call and have a look .
We can't understand this , So we're going to Binary call See if you can understand .
We don't seem to understand the two-level system , So let's see 10000 stay How to store... In memory . .
We found that what is stored in memory seems to be the same as what is stored in binary . And it takes up 4 Bytes . We know that the display in text form is 10000 yes 5 Characters , That is to say 5 Bytes , In memory and binary, it is 4 Bytes .
Judgment of the end of file reading
Keep in mind : During file reading , Out-of-service feof The return value of the function is directly used to determine whether the end of the file .It is Apply when the file reading is finished , The judgment is that the read failed and ended , Or end of file .1. Whether the reading of text file is finished , Determine whether the return value is EOF ( fgetc ), perhaps NULL ( fgets )for example :fgetc Judge whether it is EOF .fgets Determine whether the return value is NULL .2. Judgment of reading end of binary file , Judge whether the return value is less than the actual number to be read .for example :fread Judge whether the return value is less than the actual number to be read .About fread Put... Under the description :The return value is the number of reads , Parameters 1 It's a storage space , Parameters 2 Is to read each size , Parameters 3 Is the number to read , Parameters 4 Is a file stream . If The number of actual reads is less than the number to be read , Then it means that the file reading is over .
Whether there is the concept of buffer :
ANSIC The standard is “ Buffer file system ” Processing of data files , The so-called buffered file system means that the system automatically creates files for programs in memory Open up a block for each file in use “ File buffer ” . Data output from memory to disk is first sent to a buffer in memory , loading When the buffer is full, it is sent to the disk together . If you read data from disk to computer , Then read the data from the disk file and input it into memory Impact area ( Fill the buffer ), And then send the data from the buffer to the program data area one by one ( Program variables, etc ). The size of the buffer According to the C The compiler system decides .Let's take a look at an example to prove ://VS2013 WIN10 Environmental testing int main() { FILE*pf = fopen(" Under Populus euphratica .txt", "w"); fputs("qwertyuiop", pf);// Put the code in the output buffer first printf(" sleep 20 second - The data has been written , Open under the Populus euphratica .txt file , Found no content in the file \n"); Sleep(20000); printf(" Refresh buffer \n"); fflush(pf);// When the buffer is flushed , Write the data in the output buffer to a file ( disk ) // notes :fflush In high version VS It can't be used on printf(" Sleep again 10 second - here , Open the poplar tree again .txt file , There's something in the file \n"); Sleep(10000); fclose(pf); // notes :fclose When closing a file , It also flushes the buffer pf = NULL; return 0; }
scanf and fscanf and sscanf
Let's compare scanf and fsacnf and sscanf function
These are the similarities between the three functions ,scanf It's called formatted input ,fscanf Is a formatted input function for all input streams ,sscanf Is to convert a string into formatted data .
printf and fprintf function sprintf
Look at the function prototype
printf Is the format output function ,fprintf Is a formatted output function for all output streams ,sprintf Is to convert a formatted data into a string .
fprintf and fscanf example
typedef struct stu { int age; char name[20]; double wight; }stu; int main() { stu s1 = { 18," Under Populus euphratica ",66.6 }; FILE *p = fopen(" Under Populus euphratica .abc", "w"); if (NULL == p) { perror("fopen:"); exit(-1); } fprintf(p, "%d %s %lf", s1.age, s1.name, s1.wight); fclose(p); return 0; }
We wrote , Now read out .
int main() { stu s2 = { 0 }; FILE *p = fopen(" Under Populus euphratica .abc", "r"); if (NULL == p) { perror("fopen:"); exit(-1); } fscanf(p, "%d %s %lf", &s2.age, s2.name, &s2.wight); printf("%d %s %lf", s2.age, s2.name, s2.wight); fclose(p); return 0; }
sscanf and sprint example :
typedef struct stu { int age; char name[20]; double wight; }stu; int main() { char arr[256] = { 0 }; stu s3 = { 18, " Under Populus euphratica ", 66.6 }; stu tmp = { 0 }; sprintf(arr, "%d %s %lf", s3.age, s3.name, s3.wight); printf(" Print arr Content :%s\n", arr); sscanf(arr, "%d %s %lf", &tmp.age, tmp.name, &tmp.wight); printf(" Print tmp Content :%d %s %lf\n", tmp.age, tmp.name, tmp.wight); return 0; }
Here, we first convert and formatted data into string output , Then convert the string into formatted data for output .
版权声明
本文为[Populus euphratica under the desert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231654308713.html
边栏推荐
- How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
- ◰ GL shader handler encapsulation
- ByteVCharts可视化图表库,你想要的我都有
- Encapsulating the logging module
- SPC introduction
- MySQL modify master database
- Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
- PostgreSQL列存与行存
- CentOS MySQL multi instance deployment
- Interface document yaml
猜你喜欢
随机推荐
RTKLIB 2.4.3源码笔记
Redis docker installation
SPC introduction
Introduction to how to set up LAN
MySQL master-slave synchronization pit avoidance version tutorial
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
Installing labellmg tutorial in Windows
JSON deserialize anonymous array / object
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
计算饼状图百分比
MySQL modify master database
_ Mold_ Board_
Shell脚本——Shell编程规范及变量
AIOT产业技术全景结构-数字化架构设计(8)
UWA Pipeline 功能详解|可视化配置自动测试
Node access to Alipay open platform sandbox to achieve payment function
织梦DEDECMS安全设置指南
Mock test using postman
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
DanceNN:字节自研千亿级规模文件元数据存储系统概述