A brief discussion on files and file operations in C language
1. Introduction to files in C language
1.1 Introduction to files
1Broadly speaking, a file is a sequence of bytes, nothing more. Every I/0 device, including disks, keyboards, monitors, and even networks, can be viewed as a file. This standardizes the definition of files.
In a narrow sense, we say that the files on the disk are files. Files in C language can also be understood in this way.
1.2 Why use files
The data of the program we write is stored in the computer's memory. If the program exits and the memory is recycled, the data will be lost. When you run the program again, you will not be able to see the data from the last program. If we want to save the data persistently, we can use files to store our program data.
1.3 Classification of C language files
From a functional perspective, we can divide files into: 1. Program files; 2. Data files.
Program files: It includes source program files (suffix .c), target files (windows environment suffix .obj), which can Execute the program (the suffix is .exe in Windows environment).
Data file: The content of the file is not necessarily the program, but the data read and written when the program is running, such as a file that needs to read data from when the program is running, or a file that outputs content. The files we operate next refer to data files. Because operating data files means we operate on the data in the disk, this is also a function we often use in daily operations.
1.4 File name
The file name contains three parts: file path + file name trunk + file suffix.
For example:
c:\code\test.txt
Generally, we call the backbone of the file name + the file suffix the file name.
2. File operations in C language
2.1 Stream and standard stream
2.1.1 Stream
2 The data of our program needs to be output to various external devices, and data needs to be obtained from external devices. The input and output operations of different external devices are different. In order to facilitate programmers to operate various devices conveniently , We have abstracted the concept of flow , and we can imagine a flow as a river flowing with characters.
The C program's data input and output operations on files, screens, keyboards, etc. all operate in the same flow.
Generally speaking, if we want to write data to a stream or read data from a stream, we must open the stream and then operate.
2.1.2 Standard stream
2 When a C language program is started, 3 streams are opened by default:
• stdin - standard input stream, which is input from the keyboard in most environments.
• stdout - the standard output stream, output to the monitor interface in most environments.
• stderr - the standard error stream, output to the monitor interface in most environments.
The types of the three streams stdin, stdout, and stderr are: FILE*, usually called file pointers.
In C language, various operations of the stream are maintained through the file pointer of FILE*.
2.2 File pointer
Each used file has a corresponding file information area opened in the memory, which is used to store the relevant information of the file (such as the name of the file, the file status and the current location of the file, etc.). This information is stored in a structure variable. The structure type is declared by the system and named FILE.
We can create a pointer variable of FILE*:
FILE* pf
Define pf as a pointer variable pointing to FILE type data. You can make pf point to the file information area of a certain file (it is a structure variable). The file can be accessed through the information in the file information area. In other words, the file associated with it can be found indirectly through the file pointer variable.
2.3 Opening and closing files
ANSIC stipulates to use the fopen function to open the file and fclose to close the file. Let’s take a look at the 3
fopen functions first :
FILE * fopen ( const char * filename, const char * mode );
//filename指的是文件名,而mode指的是访问的模式
The values of 2 mode are as follows:
model | describe | If the specified file does not exist |
---|---|---|
"r" (read only) | To enter data, open an existing text file | Error |
"w" (write only) | To output data, open a text file | Create a new file |
“a” (add) | Add data to the end of text file | Create a new file |
"rb" (read-only) | To enter data, open a binary file | Error |
"wb" (write only) | To output data, open a binary file | Create a new file |
“ab” (add) | Add data to the end of a binary file | Create a new file |
"r+" (read and write) | For reading and writing, open a text file | Error |
"w+" (read and write) | For reading and writing, create a new file | Create a new file |
"a+" (read and write) | Open a file and read and write at the end of the file | Create a new file |
"rb+" (read and write) | Open a binary file for reading and writing | Error |
"wb+" (read and write) | For reading and writing, create a new binary file | Create a new file |
"ab+" (read and write) | Open a binary file and read and write at the end of the file | Create a new file |
You can try the following code, uncomment it and observe the results of your own file operations:
#include <stdio.h>
int main()
{
//FILE* fp = fopen("text.txt", "w");//只写 选取好对应的模式
//FILE* fp = fopen("text.txt", "r");//只读
//FILE* fp = fopen("text.txt", "a");//追加
//FILE* fp = fopen("text.txt", "rb");//只读 二进制
FILE* fp = fopen("text.txt", "wb");//只写 二进制 重新写入
//FILE* fp = fopen("text.txt", "ab");//追加 二进制
//FILE* fp = fopen("text.txt", "r+");//读写
//FILE* fp = fopen("text.txt", "w+");//读写
//FILE* fp = fopen("text.txt", "a+");//读写 追加
//FILE* fp = fopen("text.txt", "rb+");//读写
//FILE* fp = fopen("text.txt", "wb+");//读写
//FILE* fp = fopen("text.txt", "ab+");//读写
if (fp != NULL)
{
fputs("fopen example", fp);//将内容传递到文件中去
fclose(fp);//关闭文件
fp=NULL;//防止内存泄露
}
else
{
perror(fopen);
}
return 0;
}
2.4 Sequential reading and writing of files
2 Functions for sequential reading and writing are:
Function name | Function | Applicable to |
---|---|---|
fgetc | Character input function | all input streams |
fputc | character output function | All output streams |
fgets | Text line input function | All output streams |
fputs | character output function | All output streams |
fscanf | Format input function | all input streams |
fprintf | Format output function | All output streams |
fread | binary input | document |
fwrite | binary output | document |
This is not about the code. For each function, it is recommended to go to www.cplusplus.com (enter the legacy version) to search for the specific use and effects. It should be noted that each function must cooperate with the different modes in the corresponding fopen function.
2.5 Introduction to random read and write functions
2.5.1 fseek
int fseek ( FILE * stream, long int offset, int origin );
The function is to locate the file pointer based on its position and offset.
2.5.2 rewind
void rewind ( FILE * stream );
The function is to return the file pointer position to the starting position of the file.
2.5.3 ftell
long int ftell ( FILE * stream );
Returns the offset of the file pointer relative to the starting position