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.
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've just finished reading the article "Function fopen () in C" edited by the TipsMake team. You can save function-fopen-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.