C fopen() Function (original) (raw)
Last Updated : 10 Jan, 2025
In C, the **fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further operations on the file, such as reading from or writing to it. If the file exists then the fopen() function opens the particular file else a new file is created in some cases.
**Let's take a look at an example:
C `
#include <stdio.h> #include <stdlib.h>
int main(){ FILE* fptr;
// Creates a file "demo_file"
// with file access as write mode
fptr = fopen("demo_file.txt", "w+");
//
fprintf(fptr, "%s", "GeeksforGeeks");
fclose(demo);
return 0;
}
`
On running the following program, a new file will be created by the name “**demo_file.txt” with the following content:
**demo_file.txt
Welcome to GeeksforGeeks
Syntax of fopen()
**fopen (filename, mode);
**Parameters:
- **filename: Name of the file to be opened (with extension)
- **mode: For what purpose file is to be opened.
**Return Value:
- Returns a FILE pointer if the file is successfully opened.
- Returns NULL if failed.
File Opening Modes
The below table lists valid mode values for feof() function 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. |
It is recommended to close the opened file after doing the required operations using fclose().
Examples of fopen()
The below examples demonstrate how to use the fopen() for opening files in different modes:
**Opening a File for Reading
C `
#include <stdio.h>
int main() {
// Open file in read mode
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error!\n");
return 1;
}
printf("Successfull\n");
// Close the file
fclose(file);
return 0;
}
`
Output
Error opening file!
**Explanation: The file example.txt is opened in read mode ("r"). If the file exists, it opens successfully. If the file doesn't exist, fopen() returns NULL, and the program outputs an error message.
**Opening a File for Writing
C `
#include <stdio.h>
int main() {
// Open file in write mode
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error!\n");
return 1;
}
fprintf(file, "Hello, this is a test GFG file.\n");
printf("Data written successfully.\n");
// Close the file
fclose(file);
return 0;
}
`
**In output.txt
Hello, this is a test GFG file.
**Explanation: The file output.txt is opened in write mode ("w"). If the file does not exist, it is created. The program writes a string to the file using fprintf(). After writing, the file is closed using fclose().
**Opening a File for Appending
C `
#include <stdio.h>
int main() {
// Open file in append mode
FILE *file = fopen("append_example.txt", "a");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Appending text\n");
printf("Data appended\n");
// Close the file
fclose(file);
return 0;
}
`
**Explanation: The file append_example.txt is opened in append mode ("a"). If the file exists, the new content is appended to the end of the file. If the file does not exist, it is created. The program appends the text using fprintf().
**Handling File Opening Failures
C `
#include <stdio.h>
int main() {
// Try to open a non-existing file
FILE *file = fopen("non_existent_file.txt", "r");
if (file == NULL) {
perror("Error");
return 1;
}
// Close the file
fclose(file);
return 0;
}
`
**Output
Error: No such file or directory
**Explanation: This code tries to open a non-existent file non_existent_file.txt in read mode ("r"). If the file doesn't exist, fopen() returns NULL. The perror() function is used to print a detailed error message explaining why the file could not be opened.