C ++ fopen function usage

A function definition

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

Second, the parameter mode:

"r" read:  input operation to open a file, the file must exist.
"w" write:  the output operation creates an empty file, if the file already exists, the contents of the file have been discarded, treated as an empty file.
"a" append:  open the file, and then output operations always append data to output file, if the file does not exist, create a new file.
"r+" read / update:  open file (input and output) update, the file must be present
"w+" write / update:  create a space for the input and output file, if the file already exists, the contents of the file have been discarded, treated as an empty file.
"a+" append / update:  Open the output file, the output operation is always then append the data file, if the file does not exist, create a new file.

Models are specified in the table to open the file in a text mode, if opened in binary form, the need to add "b" in the mode, either mode at the end of the string (e.g., "rb +"), may be in two intermediate characters (e.g., "r + b").

Third, the return value

If the file is opened successfully, returns a pointer to a FILE object pointer, otherwise returns NULL;

Fourth, the code

 1 #include <cstdio>
 2 using namespace std;
 3 int main()
 4 {
 5     FILE *pFile;
 6     pFile=fopen("myfile.txt","w");
 7     if(pFile!=NULL)
 8     {
 9         fputs("it's a fopen example",pFile);
10         fclose(pFile);
11     }
12     return 0;
13 }

Guess you like

Origin www.cnblogs.com/jianqiao123/p/12156258.html