Header files: #include <stdio.h>
fopen () is a common function used to open a file in a specified manner, with the following prototype:
FILE * fopen (const char * path, const char * mode);
"Parameter" path is the file name that contains the path, mode is the file open mode.
Mode is available in the following ways:
Open Mode |
Description |
R |
To open a file as read-only, the file must exist. |
r+ |
Open the file as read/write, the file must exist. |
rb+ |
Opens a binary file in read/write mode, allowing only read/write data. |
rt+ |
Open a text file in read/write mode, allowing read and write. |
W |
Open a write-only file, if the file exists, the length is clear to 0, that is, the contents of the file disappears, if not present, create the file. |
w+ |
Open a readable/writable file, and if the file exists, the file length is zero, that is, the file content disappears. If the file does not exist, the file is created. |
A |
Open the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained (EOF character). |
A + |
Open a readable/writable file in an additional way. If the file does not exist, the file is created, and if the file exists, the written data is added to the end of the file, i.e. the original content of the file is retained (the old EOF character is not retained). |
Wb |
Open or create a new binary file in write-only mode, allowing only write data. |
wb+ |
Open or create a binary file in read/write mode, allowing both read and write. |
wt+ |
Open or create a text file in read/write mode to allow read and write. |
at+ |
Opens a text file in read/write mode, allowing reading or appending data at the end of the text. |
ab+ |
Opens a binary file in read/write mode, allowing reading or appending data at the end of the file. |
The b character is ignored in the POSIX system, which contains Linux. A new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.
The difference between binary and text modes:
- In the Windows system, in text mode, the file represents a newline with "\ r \ n". If you open a file in text mode and write a newline character "\ n" with a function such as fputs, the function automatically adds "\ r" before "\ n". That is, "\ r \ n" is actually written to the file.
- In the text mode of the class Unix/linux system, the file represents a line break with "\ n". Therefore, there is no difference between the text mode and the binary mode in the Linux system.
For more information, see: C language fopen () the difference between opening a text file and a binary file
Some c-compiler systems may not fully provide all of these features, and some C versions do not use "r+", "w+", "A +", but with "RW", "WR", "AR" and so on, readers pay attention to the rules of the system used.
When the return value file is opened successfully, the file pointer to the stream is returned. If the file fails to open, it returns null and the error code exists in errno.
Note: In general, the file will be opened after some file read or write action, if the file fails to open, the next read and write actions can not be smooth, so after fopen () Please make error judgment and processing.
After the file operation is complete, you need to close the file, be aware that this will cause the file to consume memory leaks and problems in the next access to the file.
When the file is closed, you need to point the file pointer to null, which prevents the free pointer from appearing, causing unnecessary hassles to the entire project, such as FP = NULL.
Instance opens a file and then closes the file.
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int main()
- {
- FILE* fstream;
- Char msg[+] = "hello! I have the read this file. ;
- FStream=fopen("test.txt","at+");
- If(fstream==null)
- {
- printf("Open file Test.txt failed! \ n");
- Exit(1);
- }
- Else
- {
- printf("Open file Test.txt succeed! \ n");
- }
- Fclose(FStream);
- return 0;
- }
C-language F (open) function (file operation/read/write)