当前位置:网站首页>File -- first acquaintance
File -- first acquaintance
2022-07-25 09:53:00 【Hills and valleys】
List of articles
Preface
When to use files ?:
When we need to persist data , Need to use files . The general methods of persisting data are : Store the data on the disk file , Store the data in the database .
Using files, we can directly store the data on the hard disk of the computer , Data persistence .
One 、 Classification of documents
Classification of documents in programming :
The classification of documents in the form of data organization :
Two 、 Some functions about files
Open file :fopen
Close file :fclose
Different opening methods of files correspond to different :
1. Opening and closing of files
The code is as follows ( Example ):
int main() {
// Open file
FILE* pf;
// Create a FILE* File pointer to
pf = fopen("class1.txt", "w");
// Use fopen Function to open a file
//"class1.txt" For file name
// Because we didn't create the file just now So use "w" The way to create a file directly in the open
// Close file
fclose(pf);
//fclose The function passes parameters to the file pointer to close pf
pf = NULL;
// Remember to set the closed file pointer to null
return 0;
}
After the code runs , You can find the file we created in the corresponding path class1.txt
2. Data input

1. Single character input
The code is as follows ( Example ):
// Writing documents
// Writing of a single character
fputc('w', pf);
// The characters in single quotation marks indicate what we want to write The back pf To give pf Write in
When the program runs , We can find that the file size just now becomes 1KB, After opening the file , You can see what we write .
2. Input of a string of characters
// Writing documents
fputs("wang", pf);
//fputs You can enter one line at a time
// Note that the file should be opened in 'w'
// The line to be entered should use double quotation marks

After program running , We can see the written content in the document wang
3. Data output
1. Single character output
// Reading documents
int ret = fgetc(pf);
printf("%c\n", ret);
// Note that here is reading file Therefore, the above file opening method should be changed to "w"
When the code runs , You can see the characters we read “w" Printed on the screen .
2. Output of a string of characters
char ret[10] = "";
fgets(ret, 3, pf);
printf("%s\n", ret);
// Note that here is reading file The file should be opened in r
//fgets The function takes three arguments
// The first parameter indicates where to put the content after reading
// The second parameter indicates reading n-1 Characters
// The third parameter indicates where to read
After the program runs, we can see what it reads n-1 Characters are printed on the screen .
4. Binary
1. Reading of binary files
fread(&pf, sizeof(char), 1, pf);
printf("%c\n", pf);
//fread Function requires four parameters
// first &fp Represents the address where the read-out data is stored
// the second sizeof(char) Represents the number of unit bytes of content to be read
// The third number 1 Delegate to read sizeof The number of bytes
// Address of the fourth read data

Here we read 1 individual size by char The data of , So printing out is stored in class1.txt The first character in .
3、 ... and 、 Judgment of the end of the document
1.EOF function
EOF Function should be used to judge when the file reading is over , Whether it ends with the end of the file or with a read error .
2. End of reading text file
Whether the reading of text file is over , application fgetc Function to determine whether the return value is EOF Or use fgets Function to determine whether the return value is NULL;
3. The reading of binary file ends
Whether the reading of binary file is over , You should use fread Function to determine whether the return value is less than the number that should be read .
边栏推荐
- 【深度学习模型部署】使用TensorFlow Serving + Tornado部署深度学习模型
- Kotlin basic knowledge points
- A picture explains SQL join left and right
- Yolov5 realizes target detection of small data sets -- kolektor defect data set
- SystemVerilog语法
- 【降维打击】希尔伯特曲线
- matlab如何导入大量数据
- First knowledge of opencv4.x ---- mean filtering
- MinkowskiEngine 安装
- Raspberry sect door ban system based on face recognition
猜你喜欢

【深度学习模型部署】使用TensorFlow Serving + Tornado部署深度学习模型

First knowledge of opencv4.x --- drawing shapes on images

Mixed supervision for surface defect detection: from weakly to fully supervised learning

Camera attitude estimation

CDA LEVELⅠ2021新版模拟题一(附答案)

基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(上)

初识Opencv4.X----ROI截取

Visualization of sensor data based on raspberry pie 4B

@3-2 optimal threshold of CCF 2020-12-2 final forecast

Creation of adjacency table of undirected connected graph output breadth depth traversal
随机推荐
Wechat applet realizes the rotation map (automatic switching & manual switching)
CCF 201509-4 高速公路
*7-2 CCF 2015-09-2 date calculation
First knowledge of opencv4.x --- box filtering
¥ 1-3 SWUST OJ 942: reverse sequence table
ADC简介
1094 - Google recruitment
Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 1)
ARMV8体系结构简介
[data mining] Chapter 3 basis of data analysis
How to install pytorch—— A most simple and effective method!
【机器翻译】SCONES——用多标签任务做机器翻译
CDA Level1复盘总结
@1-1 CCF 2021-04-1 gray histogram
Segmentation-based deep-learning approach for surface-defectdetection(基于分割的表面缺陷深度学习检测方法)
深入理解pytorch分布式并行处理工具DDP——从工程实战中的bug说起
Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
【深度学习】自编码器
expect+sh实现自动交互
服务器cuda toolkit多版本切换