The fopen() method in C is a library function that is used to open a file to perform various operations which include reading, writing, etc. along with various modes. If the file exists then the fopen() function opens the particular file else a new file is created.
Syntax
The syntax of C fopen() is:
FILE *fopen(const char *file_name, const char *mode_of_operation);
Parameters
The method accepts two parameters of character pointer type:
- file_name: This is of C string type and accepts the name of the file that is needed to be opened.
- mode_of_operation: This is also of C string type and refers to the mode of the file access.
The below table lists valid mode_of_operation values in C with their meaning:
Opening Modes | Description |
---|
r | Searches file. Opens the file for reading only. If the file is opened successfully fopen() loads it into memory and sets up a pointer that points to the first character in it. If the file cannot be opened fopen() returns NULL. |
w | Searches file. If the file exists already, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. It creates a new file for writing only(no reading). |
a | Searches file. If the file is opened successfully fopen() loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. The file is opened only for appending(writing at the end of the file). |
r+ | Searches file. Opens the file for both reading and writing. If opened successfully, fopen() loads it into memory and sets up a pointer that points to the first character in it. Returns NULL, if unable to open the file. |
w+ | Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. The difference between w and w+ is that we can also read the file created using w+. |
a+ | Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open the file. The file is opened for reading and appending(writing at the end of the file). |
rb | Open the binary file in read mode. If the file does not exist, the open() function returns NULL. |
wb | Open the binary file in write mode. As the pointer is set to the start of the file, the contents are overwritten. If the file does not exist, a new file is created. |
ab | Open the binary file in append mode. The file pointer is set after the last character in the file. A new file is created if no file exists with the name. |
rb+ | Open the binary file in read and write mode. If the file does not exist, the open() function returns NULL. |
wb+ | Open the binary file in read and write mode. Contents are overwritten if the file exists. It will be created if the file does not exist. |
ab+ | Open the binary file in read and append mode. A file will be created if the file does not exist. |
Return Value
- The function is used to return a pointer to FILE if the execution succeeds else NULL is returned.
Example of fopen()
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * demo;
demo = fopen ( "demo_file.txt" , "w+" );
fprintf (demo, "%s %s %s" , "Welcome" , "to" ,
"GeeksforGeeks" );
fclose (demo);
return 0;
}
|
On running the following command, a new file will be created by the name “demo_file.txt” with the following content:
Welcome to GeeksforGeeks
Now if we wish to look into the file then we need to run the following code, which will open the file and display its content.
C
#include <stdio.h>
int main()
{
FILE * demo;
int display;
demo = fopen ( "demo_file.txt" , "r" );
while (1) {
display = fgetc (demo);
if ( feof (demo))
break ;
printf ( "%c" , display);
}
fclose (demo);
return 0;
}
|
Output
Welcome to GeeksforGeeks
More articles on File Handling in C:
- Basics of File Handling in C
- fopen() for an existing file in write mode
- EOF, getc() and feof() in C
- File opening modes(r versus r+)