当前位置:网站首页>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 !
边栏推荐
- Mobile web
- [day_060423] no two
- Interpretation of TPS motion (cvpr2022) video generation paper
- CCTV dialogue ZTE: why must the database be in your own hands?
- [day_030420] numbers that appear more than half of the time in the array
- Cdga | how to build data asset catalogue?
- K. Link with Bracket Sequence I dp
- Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
- [1] Basic knowledge of mathematical modeling
- 【Day_07 0425】Fibonacci数列
猜你喜欢

How can machinery manufacturing enterprises do well in production management with the help of ERP system?

Map collection inheritance structure

【pytorch】图片增广
![[C language] file operation](/img/19/5bfcbc0dc63d68f10155e16d99581c.png)
[C language] file operation
![[day05_0422] C language multiple choice questions](/img/cb/0019ec819480bd9f52055e726b323a.png)
[day05_0422] C language multiple choice questions

性能测试包括哪些方面?分类及测试方法有哪些?

Upgrade appium automation framework to the latest 2.0

BPG笔记(四)

Code runner for vs code, with more than 40million downloads! Support more than 50 languages

Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
随机推荐
性能测试包括哪些方面?分类及测试方法有哪些?
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
Distributed | practice: smoothly migrate business from MYCAT to dble
[day_050422] continuous maximum sum
[day03_0420] C language multiple choice questions
nuxt 配置主题切换
[day_030420] numbers that appear more than half of the time in the array
Widget is everything, widget introduction
【Day_02 0419】排序子序列
Leetcode 347. top k high frequency elements
Ganglia安装部署流程
Interpretation of TPS motion (cvpr2022) video generation paper
Workflow activiti5.13 learning notes (I)
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
[day06_0423] C language multiple choice questions
CV (1)- Introduction
【pytorch】图片增广
【Day04_0421】C语言选择题
Flex layout
[day04_0421] C language multiple choice questions