当前位置:网站首页>C language file operation
C language file operation
2022-07-26 06:23:00 【Science52】
In this issue, I will explain to you the knowledge of documents , Now let's enter the learning journey
1. Why use files
When we learned about structures earlier , The program that wrote the address book , When the address book runs , You can add... To the address book 、 The number of deletions According to the , At this time, the data is stored in memory , When the program exits , The data in the address book naturally does not exist , Wait until the next time you run the communication When recording the program , The data has to be re entered , It's hard to use such an address book . We were thinking that since it was an address book, we should record the information , Only when we choose to delete data , The data no longer exists . This involves the problem of data persistence , Our general methods of data persistence are , Store the data in a disk file 、 Stored in data Library, etc . Using files, we can store data directly on the hard disk of the computer , Data persistence is achieved .
2. What is a document
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 ).
2.1 Program files
Include source files ( The suffix is .c), Target file (windows Environment suffix is .obj), Executable program (windows Environmental Science The suffix is .exe).
2.2 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 file .
Here we mainly discuss data files
2.3 file name
A file should have a unique file ID , So that users can identify and reference . The filename contains 3 part : File path + File name trunk + file extension for example : c:\code\test.txt For convenience , The file ID is often referred to as the file name .
3. Opening and closing of files
3.1 The file pointer
Buffer file system , The key concept is “ File type pointer ”, abbreviation “ The file pointer ”. Each used file has a corresponding file information area in memory , It is used to store information about files ( Such as file name word , File status and current file location, etc ). This information is stored in a structure variable . The structure type is systematic Declarative , The name FILE

Definition pf It's a point FILE Pointer variable of type data . You can make pf Point to the file information area of a file ( It's a structural change The amount ). Through the information in the file information area, you can access the file . in other words , Through the file pointer variable, you can find the associated The file of .

3.2 Opening and closing of files
The file should be opened before reading and writing , The file should be closed after use . 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 .


Here are the main opening methods :


After opening successfully, a file will be generated

4. Sequential reading and writing of files

Now I'll teach you how to use a function fputc:


Other functions are used in the same way , But their functions are different
4.1 Compare a set of functions :

scanf It is a formatted input statement for standard input
printf It is a formatted output statement for standard output
fscanf It is a formatted input statement for all standard input streams
fprintf It is a formatted output statement for all standard output streams
sprintf Is to convert a formatted data into a string .
sscanf Is converted from string to formatted data .
5. Random reading and writing of documents
5.1 fseek
Locate the file pointer according to its position and offset .


5.2 ftell
Returns the offset of the file pointer from its starting position


5.3 rewind
Return the file pointer to the beginning of the file


6. Text files and binaries
According to the organization of data , Data files are called text files or binary files .
Data is stored in memory in binary form , If the output without conversion is to external memory , Binary files .
If it's required to use ASCII In the form of code , You need to convert before storing .
With ASCII The file stored in the form of characters is text This document .
How is a data stored in memory ?
All characters are written in ASCII stored , Numerical data can be used either ASCII stored , It can also be stored in binary form . If there are integers 10000, If the ASCII Code output to disk , The disk is occupied by 5 Bytes ( One byte per character ), and Binary output , On the disk 4 Bytes


Let's test our conjecture :

Here we find that these data have been incomprehensible , The following is another world opened in binary mode

because VS Small end storage is the reverse
7. Determination of the end of file reading
7.1 Misused feof
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 .
Instead, it applies when the file reading ends , 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 .
Examples of text files :

Examples of binary files :

8. File 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 .


This code proves that the file buffer does exist .
Because there is a buffer ,C Language when operating files , You need to flush the buffer or close the file at the end of the file operation Pieces of . If you don't do , May cause problems in reading and writing files .
This period ends here , Thank you for your support !
边栏推荐
- Leetcode 42. rainwater connection
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- [pytorch] CNN practice - flower species identification
- [day04_0421] C language multiple choice questions
- 【pytorch】CNN实战-花朵种类识别
- Widget is everything, widget introduction
- [day_020419] inverted string
- 基于消防GIS系统的智慧消防应用
- [Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
- BPG notes (IV)
猜你喜欢

Multi target detection

Intelligent fire protection application based on fire GIS system

Implementation of PHP multitask second timer

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

RNN recurrent neural network

Redis sentinel cluster setup

Convolutional neural network (II) - deep convolutional network: case study
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

Swift basic FileManager (file management)
随机推荐
PG Vacuum 杂谈之 auto vacuum
【Day04_0421】C语言选择题
Multi target detection
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
[Hangzhou][15k-20k] medical diagnosis company recruits golang development engineers without overtime! No overtime! No overtime!
redis 哨兵集群搭建
The number of weeks of Oracle last year and this year, with the start time and end time
What is spark serialization for?
[day_020419] sort subsequence
【Day_07 0425】合法括号序列判断
[pytorch] picture enlargement
Convolutional neural network (II) - deep convolutional network: case study
2022年下半年系统集成项目管理工程师(软考中级)报名条件
[day_050422] continuous maximum sum
C语言进阶——可存档通讯录(文件)
BigDecimal变为负数
Why use the static keyword when defining methods
English sentence pattern reference exclusive Edition - adverbial clause
Matlab 向量与矩阵
Alibaba cloud OSS binding custom domain name