How to use the fopen() function

grammar:

FILE *fopen(const char *filename, const char *mode);

return value:

The fopen function returns the file pointer of the newly opened file ;
if the file cannot be opened, it returns a NULL pointer .

The table is clearly visible:

model describe Whether the file exists
"r" Open file for reading only must exist
"r+" open the file for reading and writing must exist
"w" Create new file for writing only If it exists, clear it and write it again
"w+" Create a new file for reading and writing If it exists, clear it and write it again
"a" Open file for reading and appending for writing If it does not exist, create a new file to write

Example questions in Niuke.com:

Parsing:
        "r": Open the file in read-only mode, the file must exist.

        "r+": Open the file for read/write, the file must exist.

        "w": Open a write-only file. If the file exists, the length will be cleared to 0, that is, the content of the file will disappear. If it does not exist, the file will be created.

        "w+": Open the file for reading/writing. If the file exists, the file length will be cleared to zero, that is, the content of the file will disappear. Create the file if it does not exist.

        "a": Open the write-only file as an append. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be preserved.

        "a+": Open the readable and writable file in an additional way. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained.

Guess you like

Origin blog.csdn.net/y1120944224/article/details/125683328