How to use fopen in c, you should know

fopen in c

The fopen in C is used to create a new file or open an existing file. It is declared in stdio.h. It has two arguments, its first argument is the name of the file to be created or opened. The second argument is the mode of the file in which it is created or opened. These modes are predefined if you are not using these modes then the behavior would be undefined.

Syntax of fopen():

FILE *fopen(const char * restrict filename,const char * restrict mode);

 

Parameters of fopen():

1. filename: pointer to a character for the file name.

2. mode: pointer to a character for file mode.

Return value of fopen in C:

The fopen function returns a pointer to the object controlling the stream. If the open
the operation fails, fopen returns a null pointer.

You might like these articles,

Example code to create a file using fopen in C:

In the below code, I am creating a text file using the fopen() function.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp = NULL;

    //create a text file
    fp  = fopen ("aticleworld.txt", "w");
    if(fp == NULL)
    {
        printf("File is not created\n");
        exit(1);
    }
    else
    {
        printf("File is created\n");
    }

    //close the file
    fclose(fp);

    return 0;
}

 

Output:

If fopen() create file successfully, the text file will be created in the same folder where you have saved your code. Like the below image.

file handling in c

 

 

If you want to create or open the file at a specified location, you have to give the path in fopen.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp = NULL;

    //create a text file
    fp  = fopen ("F://data.txt", "w");
    if(fp == NULL)
    {
        printf("File is not created\n");
        exit(1);
    }
    else
    {
        printf("File is created\n");
    }
    //close the file
    fclose(fp);

    return 0;
}

 

Output:

If fopen() create file successfully, the text file will be created F drive. You can see the below image.

C file handling

 

A new mode, introduced by C11 is “x” which is an exclusive create-and-open mode. Mode “x” can be used with any “w” specifier, like “wx”, “w+x”, “wbx” or “wb+x”.  Opening a file with exclusive mode (“x”) fails if the file already exists or cannot be created.

Let see an example,

Using the above-mentioned code, I have already created a text file. If I will use “w+x” mode and try to overwrite the file, then fopen returns NULL and prevent from the file overwriting.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp = NULL;

    //create a text file
    fp  = fopen ("F://data.txt", "w+x");
    if(fp == NULL)
    {
        printf("File is not created\n");
        exit(1);
    }
    else
    {
        printf("File is created\n");
    }
    //close the file
    fclose(fp);

    return 0;
}

 

Output:

 

Recommended Articles for you:

 



Leave a Reply

Your email address will not be published. Required fields are marked *