C fopen() Function With Examples

C fopen() Function With Examples

The fopen() method in C is a library function that functions 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 will open the particular file a new file is created.

Syntax

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

Parameters

The method takes two parameters, both of character pointer type:

  1. file_name: This parameter, of C string type, holds the name of the file to be opened.
  2. mode_of_operation: Also a C string type, this parameter indicates the mode of file access, specifying whether the file will be opened for reading, writing, or both.

 Table lists valid mode_of_operation values in C with their meaning

Opening ModesDescription
rThis function will Search the file. It will Open 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.
wThis function will Search the file. If the file exists already, its contents will be overwritten. If the file doesn’t exist, a new file is made. Returns NULL, if unable to open the file. It creates a new file for writing only(no reading).
aThis function will Searches the 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.
r+This function will search the file. Opens the file for both reading and writing. If it is opened successfully, open f () 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+This function will Search the file. If the file exists, its contents will be overwritten. If the file doesn’t exist, a new file will be formed. Returns NULL, if unable to open the file. The difference between w and w+ is that we can also read the file made with the w+.
a+it functions to Search files. 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 will be formed. Returns NULL, if unable to open the file. The file is opened for reading and appending(writing at the end of the file).
rbIt functions to Open the binary file in read mode. If the file does not exist, the open() function returns NULL.
wbIt functions to 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 formed.
abIt functions to Open the binary file in append mode. The file pointer is set after the last character in the file. A new file will be made  if no file exists with the name.
rb+It functions to Open the binary file in append mode. The file pointer is set after the last character in the file. A new file will be made if no file exists with the name.
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()

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




// 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 access as write-plus mode
    demo = fopen("demo_file.txt", "w+");
 
    // adds content to the file
    fprintf(demo, "%s %s %s", "Welcome", "to",
            "Skillvertex");
 
    // closes the file pointed by demo
    fclose(demo);
 
    return 0;
}

According to the command , a new file will be produced by the name “demo_file.txt” with the following content: 

Welcome to Skillvertex

To view the file, Run the following code, which will open the file and display its content.

// 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 access 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;
}

Output

Welcome to Skillvertex

FAQ- C fopen() Function With Examples

Q1. What does fopen () function do in C?

Ans. The fopen() function is used to open a file identified by its filename and establish a connection to it through a stream. The mode variable, represented as a character string, defines the type of access that is requested for the file, such as reading, writing, or both.

Q2. What is the return type of the fopen () function in C *?

Ans. The fopen() function returns a pointer to a FILE structure type, enabling access to the opened file. This pointer is crucial for performing operations on the file, such as reading or writing. It’s important to note that when working with stream files of type record, you may need to cast the FILE pointer to an RFILE pointer to use record I/O functions effectively.

Q3. What is the difference between fopen and open in C?

Ans. The fopen() function in C returns a FILE* (file pointer), which is used with functions like fprintf(), fscanf(), etc. On the other hand, the open() system call returns a file descriptor (an integer), which is often used with low-level functions like read() and write()

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment