当前位置:网站首页>Basic knowledge of documents
Basic knowledge of documents
2022-07-25 18:10:00 【Swordsman】
Knowledge about documents
Classification of documents
- Program files : image
.cSource file for suffix ,.objTarget file for suffix ,.exeExecutable program with suffix . - Data files : Data when the program reads and writes .
Or according to another classification
The documents are divided into :1. text file .2. Binary .
Data is stored in binary form in memory , Output directly to external memory without conversion , Binary files ; If conversion is added , With ASCII Standard storage is text files .
Data files
file name : The unique identification of a file .
Include : File path , File name trunk , file extension
Opening and closing of files
- The file pointer
The file pointer is actually a structure variable name FILE, The members inside are used to store the relevant information of the file . These information together constitute the file information area of each different file .
Every time you open a file , The system will create different FILE, adopt FILE* Create a variable pf, there pf It's a file pointer , It can point to the corresponding file .
- Opening and closing of documents
Before reading and writing files , The file should be opened ; When the relevant operation is over , The file should be closed .
The standard specifies the use of fopen To open the file , use fclose To close the file .
fopen
FILE* fopen(const char* filename, const char* mode);
When the file is successfully opened , What is returned is a pointer to the structure of a specific file information area ; Open failure returns a null pointer , therefore , When using files , We have to judge .
How to open some important files
| How to open the file | Specific meaning | file does not exist |
|---|---|---|
| “r” | To input data, open the file | Report errors |
| “w” | To output data, open a file | Create a new file |
| “rb” | To enter data , Open a binary file | Report errors |
| “wb” | To output data , Open a binary file | Create a new file |
fclose
int fclose(FILE* stream);
give an example
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return;
}
// Document related operations
//...
fclose(pf);
return 0;
}
Sequential reading and writing of files
| function | Function name | Using range |
|---|---|---|
| Character input | fgetc | All input streams |
| Character output | fputc | All input streams |
| Text input | fgets | All input streams |
| Text output | fputs | All input streams |
| Format input | fscanf | All input streams |
| Format output | fprintf | All input streams |
| Binary input | fread | file |
| Binary output | fwrite | file |
Random reading and writing of documents
fseek
int fseek(FILE* stream, long offset, int origin);
This function is 3 Parameters , The first is a file pointer , The second is the offset , The third is the starting position .
If the starting position is the first element, it is 0, The last element is -1.
Through this 3 Parameters can be located to the relevant location of the file , Read and write .
ftell
long ftell(FILE* stream);
If the function returns normally , Returns the offset of the file pointer from the starting position .
rewind
void rewind(FILE* stream);
Let the file pointer point to the starting position of the file .
Judgment of the end of file reading
feof
int feof(FILE* stream);
This function is used to end the file , Judge whether the reading fails or the end of the file is encountered .
- Judge whether the reading of the text file is over :
fgetc Judge whether it is EOF
fgets Judge whether it is NULL
- Judge whether the reading of binary file is over
Judge whether the return value is less than the actual number to be read ,fread Judge whether the return value is less than the actual number to be read .
边栏推荐
猜你喜欢

Auditing related notes

How to choose digital twin visualization platform

tkinter GUI版通信录管理系统

Kendryte K210 在freertos上的lcd屏幕的使用

为什么数字化未来取决于3D实时渲染

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

Lwip之内存与包缓冲管理

绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码

How to judge the performance of static code quality analysis tools? These five factors must be considered

云流化和云桌面有什么关系
随机推荐
CVE-2022-33891 Apache spark shell 命令注入漏洞复现
Auditing相关注解
有没有什么不起眼却挣钱的副业?
Stm8s003f3 internal flash debugging
Lwip之内存与包缓冲管理
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
虚拟偶像代言产品出问题谁负责?
数二2010真题考点
Go defer and recover simple notes
UnitTest框架应用
Is there any inconspicuous but profitable sideline?
Redis source code and design analysis -- 16. AOF persistence mechanism
Installation and operation instructions of SVN client (TortoiseSVN)
Redis source code and design analysis -- 15. RDB persistence mechanism
如何选择数字孪生可视化平台
MATLAB中join函数使用
RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制
List转换问题
Cloud XR面临的问题以及Cloud XR主要应用场景
itextpdf实现多PDF文件合并为一个PDF文档