
C Language File Knowledge Summary

Hello
Everyone!
We are starting a new learning session.
This session’s content is about
C Language File Knowledge.

Fixed Layout
Set fixed width and height on the toolbar,
The background can be set to be contained, perfectly aligning the background image and text,
and used for template creation.
Preface
Fixed Layout
Set fixed width and height on the toolbar,
The background can be set to be contained, perfectly aligning the background image and text,
and used for template creation.
“
Files are an essential part of data storage on the hard disk. Therefore, we should have some understanding of file-related knowledge and be proficient in using related file functions to implement code compilation and file reading.
”

01
What is a File


Basic Concept of Files
Broadly speaking, files on a disk are all files. However, in program design, we divide files into two categories: program files and data files.
Program Files: Include source program files (with .c extension), object files (with .obj extension in Windows environment), and executable programs (with .exe extension in Windows environment).
Data Files: The content of the file is not necessarily a program; it can also be data read and written during program execution, such as files from which the program needs to read data or files that output content.
File Identification
A file must have a unique file identifier for user recognition and reference. The file identifier consists of three parts: file path + file name + file extension.

File Types
Based on the organization of data, data files are further divided into text files and binary files.
Text Files: If the requirement is to store in ASCII format on external storage, it needs to be converted before storage. Files stored in ASCII characters are text files.
Binary Files: Data is stored in binary form in memory; if output to external storage without conversion, it is defaulted as a binary file.
File Control Block
To describe information related to a file, each used file allocates a corresponding file control block in memory to store relevant information. This information is stored in a structure variable. The structure type is declared by the system and named FILE.



02
File Operation Functions


File Open Function – fopen
Opens a file in a specific way. The operating system will find or create the file based on the path in the first parameter, create a new file control block to store the file information, and finally return the address of the file control block.

File Close Function – fclose
When we open a file using the fopen function, the operating system dynamically creates the file control block for this file. If it is not used, we need to use the fclose function to release it. Note that this does not delete the file; the file remains unchanged, only the file control block created by fopen is deleted.
Reading and Writing Files
Reading and writing file data can be divided into sequential and random read/write. Sequential read/write means writing and reading data in the file character by character in order.

End Determination
When using functions to read file content, there are two special cases for reading to end: an error occurs during reading or reaching the end of file marker, both return the same value, but they set different identifiers, which can be checked and distinguished using ferror and feof.




This session’s learning ends here~
Did you gain anything?
See you next time~


Images and Text | Mo Jiahua
Typesetting | Mo Jiahua
Initial Review | Zhang Jiehui
Second Review | Yang Shanyou
Wang Fei
Final Review | Huang Sheng
