当前位置:网站首页>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 .
边栏推荐
- pytorch使用tensorboard实现可视化总结
- 单目深度估计自监督模型Featdepth解读(下)——openMMLab框架使用
- MLOps专栏介绍
- 【RNN】剖析RNN 之 从RNN-(Simple|LSTM) 到 序列生成 再到 seq2seq框架(encoder-decoder,或称为seq2seq)
- 【深度学习】自编码器
- Raspberry sect door ban system based on face recognition
- ADC介绍
- How to add other PHP versions to MAMP
- Segmentation-based deep-learning approach for surface-defectdetection(基于分割的表面缺陷深度学习检测方法)
- Creation of adjacency matrix of undirected connected graph output breadth depth traversal
猜你喜欢

yolov5实现小数据集的目标检测--kolektor缺陷数据集

AMD EPYC 9664旗舰规格曝光:96核192线程 480MB缓存 3.8GHz频率
![[deep learning] self encoder](/img/7e/c3229b489ec72ba5d527f6a00ace01.png)
[deep learning] self encoder

FLASH read / write operation and flash upload file of esp8266

First acquaintance with opencv4.x --- ROI interception

手持振弦VH501TC采集仪传感器的连接与数据读取

pytorch使用tensorboard实现可视化总结

初识Opencv4.X----为图像添加椒盐噪声

初识Opencv4.X----为图像添加高斯噪声

【降维打击】希尔伯特曲线
随机推荐
从鱼眼到环视到多任务王炸——盘点Valeo视觉深度估计经典文章(从FisheyeDistanceNet到OmniDet)(上)
CDA Level1多选题精选
T5论文总结
基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(下)
First knowledge of opencv4.x --- box filtering
Some usages of Matlab's find() function (quickly find qualified values)
无向连通图邻接表的创建输出广度深度遍历
TensorFlow raw_rnn - 实现seq2seq模式中将上一时刻的输出作为下一时刻的输入
ISP图像信号处理
CUDA explanation - why GPU is used in deep learning
鱼眼图像自监督深度估计原理分析和Omnidet核心代码解读
Expect+sh realize automatic interaction
初识Opencv4.X----图像直方图绘制
深入理解pytorch分布式并行处理工具DDP——从工程实战中的bug说起
C语言基础
How to install pytorch—— A most simple and effective method!
一个硬件攻城狮的经济学基础
【Tensorflow2安装】Tensorflow2.3-CPU安装避坑指南!!!
How to import a large amount of data in MATLAB
ARM GIC简介