Table of Contents
This guide covers function fopen ()() in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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 append
Returns 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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.