Function fopen () in C
The function FILE * fopen (const char * filename, const char * mode) in Library C opens the file pointed to the filename parameter by using the given mode.
Declare function fopen () in C
Below is the declaration for the function fopen () in C:
FILE * fopen ( const char * filename , const char * mode )
Parameters
filename - This is the string containing the file name to be opened.
mode - This is the string containing a file access mode. It includes:
mode Description "r" Open a file to read. File must exist "w" Create an empty file to write. If a file with the same name already exists, its content is removed and the file is treated as a new blank file "a" Append to a file. With write operations, append data at the end of the file. The file is created if it does not already exist "r +" Open a file to write and read. File must exist "w +" Create an empty file to write and read "a +" Open a file to read and appendReturns the value
This function returns a FILE pointer. Otherwise, NULL is returned and the errno global variable is set to indicate an error.
For example
The following C program illustrates the usage of the function fopen () in C:
#include #include int main () { FILE * fp ; fp = fopen ( "baitapc.txt" , "w+" ); fprintf ( fp , "%s %s %s %s" , "Chung" , "toi" , "la" , "QTMTeam" ); fclose ( fp ); return ( 0 ); }
Compiling and running the above program will create a baitap.t.txt
Now follow the contents of the above file by using the following C program:
Compile and run the above C program to see the results:
According to Tutorialspoint
Previous post: Function fgetpos () in C
Next lesson: Function fread () in C
You should read it
- Function vfprintf () in C
- The function setvbuf () in C
- The sprintf () function in C
- Remove () function in C
- Function setbuf () in C
- Function fflush () in C
- The ferror () function in C
- The function fsetpos () in C
- Function fwrite () in C
- Function fseek () in C
- The function fgetpos () in C
- Function ftell () in C