当前位置:网站首页>[C language] document operation
[C language] document operation
2022-04-23 09:02: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 : Knowledge of file operation
I hope everyone will study and work happily every day .
What is a file ? Why do you need documents ? What is the filename ?
The files on disk are files , But in programming , There are two kinds of documents we usually talk about : Program files 、 Data files ( Classified from the perspective of file function ).Program files :Include source files ( The suffix is .c ) , Target file ( windows Environment suffix is .obj ) , Executable program ( windows Environment suffix is .exe ).Data files :The content of the file is not necessarily a program , It's the data that the program reads and writes when it runs , For example, the file from which the program needs to read data , Or output content fileThis is called file .Our general methods of data persistence are , Store the data in a disk file 、 Stored in the database . Using files, we can store data directly on the hard disk of the computer , Data persistence is achieved , That's why we use files .A file should have a unique file ID , So that users can identify and reference , The document ID contains 3 part : File path + File name trunk + file extension , Generally, we also call the file ID file name .
The concept of flow :
Flow is a channel formed between a system and a program . The two are one-to-one , Flow expresses the ability to interact between the two . So-called “ANSI C When the program is running, the system must provide three streams ,stdin,stdout,stderr”, In other words, the system must provide these three “ passageway ”, namely
1, Program and standard input ( Default keyboard ) The passage between stdin
2, Program and standard output ( The default display ) The passage between stdout
3, Program and terminal ( The default is also the display ) The passage between stderr
And we need to Today's focus is on file flow , use FILE* To point to Memory for subsequent operations .
Opening and closing of files
Files should be read and written first Open file , After use, you should Close file . In programming , While opening the file , Will return to one FILE* A pointer variable to the file , It is also equivalent to establishing a finger Relationship between needle and document . ANSIC To prescribe the use of fopen Function to open a file ,fclose To close the file .![]()
![]()
![]()
fopen The front is the file name , You need some specific strings later , and fclose Just close the file pointer variable .
File read and write operations
We can't just know how to open and close files , We should also know how to read and write files .
The above is the commonly used file operation function
Now let's practice :
Put the numbers 0~9 write file :
int main() { FILE *p = fopen(" Populus euphratica .dat", "w"); if (NULL == p) { perror("main:"); exit(-1); } for (char i = '0'; i <= '9'; i++) { fputc(i,p); } fclose(p); p = NULL; return 0; }
The open file is in write only form "w" , The writing character uses fputs function , Then I used a loop to 0~9 Each character is inserted into the file separately .
Read the contents of the file
int main() { FILE *p = fopen(" Populus euphratica .dat", "r"); if (NULL == p) { perror("main:"); exit(-1); } char a[15] = { 0 }; fgets(a, 11, p); puts(a); fclose(p); p = NULL; return 0; }
Here's a small problem , Why? fgets Function to use 11 Well ? Altogether 10 A digital , Read 11 Time ?
Because the default copy '\0' , Is to copy the end of string flag , You can see through debugging .
It can be seen that , If it's a copy 10 Time , Then just copy to 8 It stopped , That is, the last one will copy by default '\0'
So use fgets when , More elements than the actual copy 1, in other words , You want to copy 10 Elements , that fgets It should be written in 11.
Read and write in binary
We want to write some confidential documents in the file , You can write... In binary in a file .
Let's take the structure as an example , To facilitate observation :
typedef struct stu { char name[20]; int age; int ID[10]; }stu; int main() { FILE *p = fopen(" Under the tree .dat", "wb"); if (NULL == p) { perror("main:"); exit(-1); } stu s1 = { " Under Populus euphratica ", 18, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } }; fwrite(&s1, sizeof(s1), 1, p); fclose(p); return 0; }
Binary write , We don't understand , But the file knows .
Let's use it Binary on Of .
int main() { FILE *p = fopen(" Under the tree .dat", "rb"); if (NULL == p) { perror("main:"); exit(-1); } stu s2 = { 0 }; fread(&s2, sizeof(s2), 1, p); fclose(p); return 0; }
So that you can get through , Binary readout , The file is to remember .
matters needing attention
1. First of all, we need to know that when a file is operated , First of all, consider Open mode , Is it read-only or write only or in other ways .
2. Judge Whether the file was opened successfully .
3. To determine how to write a file , Whether to enter one by one or by line , Or output , These functions are different .
4. At the end of the day Close file .
Next up :
The next issue will go on to explain the knowledge of document series .
The next issue is wonderful ~!~!~!

版权声明
本文为[Populus euphratica under the desert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230723099250.html
边栏推荐
- Test your machine learning pipeline
- 是否完全二叉搜索树 (30 分)
- Pctp test experience sharing
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- 数字政府建设中政务中台中的技术创新点
- Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
- On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
- bashdb下载安装
- 单片机数码管秒表
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
猜你喜欢
随机推荐
资源打包关系依赖树
Consensus Token:web3. 0 super entrance of ecological flow
玩转二叉树 (25 分)
Matlab draw five-star red flag
Go语言自学系列 | golang结构体作为函数参数
Flink同时读取mysql与pgsql程序会卡住且没有日志
GUI编程简介 swing
npm ERR! network
Production practice elk
bashdb下载安装
Go language self-study series | golang nested structure
【原创】使用System.Text.Json对Json字符串进行格式化
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Enterprise wechat application authorization / silent login
Is Zhongyan futures safe and reliable?
Brief steps to build a website / application using flash and H5
BK3633 规格书
Taxable income
是否同一棵二叉搜索树 (25 分)
Technological innovation in government affairs in the construction of Digital Government
















