Introduction to Functions
function function: open a file
function Prototypes: FILE * fopen (const char * path,const char * mode);
related functions: Open,fclose, fopen_s[1], _wfopen
Required Libraries: <stdio.h>
return value: The file pointer to the stream will be returned when the file is opened successfully. If the file fails to open, it returns null and the error code exists in errno.
In general, after opening the file will make some file read or write action, if the file fails to open, the next read and write actions will not be smooth, so after fopen () Please make error judgment and processing.
Parameter description:
The parameter path string contains the file path and filename to open, and the parameter mode string represents the flow pattern.
Mode has the following pattern strings:
R to open a read-only file , the file must exist.
r+ open a writable file, the file must exist.
rb+ Read and write open a binary file, allowing only read and write data.
rt+ Read and write open a text file that allows reading and writing.
W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.
w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.
A write-only file opens 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 reserved)
A + opens readable and writable files 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. (the original EOF character is not preserved)
WB only writes Open or create a new binary file; Only write data is allowed.
wb+ read-write open or create a binary file that allows reading and writing.
wt+ read-write open or create a text file;
at+ Read and write open a text file that allows you to read or append data at the end of the text.
ab+ Read and write open a binary file that allows you to read or append data at the end of the file.
These morphological strings can be added with a B character, such as RB, w+b, or ab+ combinations, and a B character is used to tell the library that the file opened is a binary file, not a plain text file. However, in a POSIX system, including Linux ignores the character. 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.
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.
fopen and read-write Identity r,r+,rb+,rt+,w+ .....