C fopen() Function

Last Updated : 20 Jul, 2026

The fopen() function is used to open a file in a specified mode, such as read, write, or append. It returns a file pointer (FILE *) that is used for performing file operations.

  • Opens an existing file or creates a new file, depending on the selected mode.
  • Returns a valid file pointer on success, or NULL if the file cannot be opened.
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 ModeSummary
rOpens an existing file for reading only. Fails if the file does not exist.
wOpens a file for writing. Creates a new file or overwrites an existing one.
aOpens a file for appending. Creates the file if it does not exist.
r+Opens an existing file for both reading and writing. Fails if the file does not exist.
w+Opens a file for reading and writing. Creates a new file or overwrites an existing one.
a+Opens a file for reading and appending. Creates the file if it does not exist.
rbOpens an existing binary file for reading only. Fails if the file does not exist.
wbOpens a binary file for writing. Creates a new file or overwrites an existing one.
abOpens a binary file for appending. Creates the file if it does not exist.
rb+Opens an existing binary file for both reading and writing. Fails if the file does not exist.
wb+Opens a binary file for reading and writing. Creates a new file or overwrites an existing one.
ab+Opens a binary file for reading and appending. Creates the file if it 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:

  • Opens the file example.txt in read mode ("r").
  • If the file does not exist, fopen() returns NULL, indicating that the file could not be opened.

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:

  • Opens the file output.txt in write mode ("w"), creating it if it does not already exist.
  • Writes data to the file using fprintf() and closes the file 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;
}

Output
Data appended

Explanation:

  • Opens the file append_example.txt in append mode ("a"), creating it if it does not exist.
  • Appends new data to the end of the file 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:

  • Attempts to open non_existent_file.txt in read mode ("r").
  • If the file cannot be opened, fopen() returns NULL and perror() displays the corresponding error message.
Comment