C fopen() function with Examples

Pre-requisite: Basics of File Handling in C

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 particular file is opened else a new file is created.

Syntax:



FILE *fopen(const char *file_name, const char *mode_of_operation);

Parameters: The method accepts two parameters of character type:

  1. file_name: This is of C string type and accepts the name of the file that is needed to be opened.
  2. mode_of_operation: This is also of C string type and refers to the mode of the file access. Below are the file access modes for C:
    1. “r” – Searches file. If the file is opened successfully fopen() loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen() returns NULL.
    2. “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.
    3. “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.
    4. “r+” – Searches file. If opened successfully, fopen() loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file.
    5. “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.
    6. “a+” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which 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.

Return Value: The function is used to return a pointer to FILE if the execution succeeds else NULL is returned.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate fopen()
  
#include <stdio.h>
#include <stdlib.h>
  
int main()
{
  
    // pointer demo to FILE
    FILE* demo;
  
    // Creates a file "demo_file"
    // with file acccess as write-plus mode
    demo = fopen("demo_file.txt", "w+");
  
    // adds content to the file
    fprintf(demo, "%s %s %s", "Welcome",
            "to", "GeeksforGeeks");
  
    // closes the file pointed by demo
    fclose(demo);
  
    return 0;
}

chevron_right


On running the following command, a new file will be created by the name “demo_file” and with the following content:

Welcome to GeeksforGeeks

Example 2: 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate fopen()
  
#include <stdio.h>
  
int main()
{
  
    // pointer demo to FILE
    FILE* demo;
    int display;
  
    // Creates a file "demo_file"
    // with file acccess as read mode
    demo = fopen("demo_file.txt", "r");
  
    // loop to extract every characters
    while (1) {
        // reading file
        display = fgetc(demo);
  
        // end of file indicator
        if (feof(demo))
            break;
  
        // displaying every characters
        printf("%c", display);
    }
  
    // closes the file pointed by demo
    fclose(demo);
  
    return 0;
}

chevron_right


Output:

Welcome to GeeksforGeeks

More articles on File Handling in C:

  1. Basics of File Handling in C
  2. fopen() for an existing file in write mode
  3. EOF, getc() and feof() in C
  4. File opening modes(r versus r+)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.