The first file handling function in c programming is fopen function which is used to open the file from the permanent storage.

fopen function in C

fopen function in C: fopen( file open) file open function in c. It is a file handling function in C.
fopen: file open function is used to open a file.
fopen opens a file from storage.

The syntax for opening file

Filepointer = fopen(“file_name”,”file_mode”);

In the above syntax for fopen function in C, Filepointer is a file object which stores the pointer to file which we are opening. we write 2 parameters in fopen function. The first parameter is file name which we want to open given with its location and the second parameter is file mode is mode of a file in which we are opening a file. Different File opening modes in C are given below in the table,

When we write fopen function with name file, then compiler checks the given file in the location of the file given if it exists then it opens the file and cursor point to start and if the file does not exist then fopen creates a new file with a file name and opens that file with cursor at the start.
If the file does not exist then fopen returns NULL.

Fopen function example

1. Open technical file in read mode.
= > fopen (“technical.txt”,”r”);

Let’s assume technical.txt file already exist with content in our directory. This function opens that file in reading mode.

2. Open seek file in write mode.
fopen(“seek.txt,”,”w”);

In the above example, we are opening a seek.txt file which does not exist. in this case, fopen creates the file open it the write mode.

File opening modes in c

There is the number of ways to open the file in C programming which is as follows:

File   Mode Meaning of Mode During Inexistence of file
r Open for reading. If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
w Open for writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb Open for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a Open for append. i.e, Data is added to end of file. If the file does not exists, it will be created.
ab Open for append in binary mode. i.e, Data is added to end of file. If the file does not exists, it will be created.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
w+ Open for both reading and writing. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
wb+ Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exists, it will be created.
ab+ Open for both reading and appending in binary mode. If the file does not exists, it will be created.

Program to demonstrate fopen function in c

#include<stdio.h>

void main()

{

FILE *fp*,*fp1;

printf ("\n opening a file in read mode ");

fp=fopen ("technical.txt","r");

if(fp ==. NULL)

{

printf ("\n file not exists:");

}

else {

printf ("\n file opened in read mode");

}

printf ("\n opening a file write mode ");

fp=fopen("seek","w");

if(fp1 == NULL)

{

printf ("\n file created");

}

getch();

}

Explanation: –

In the above program, we are opening 2 files.
1. First, we open file in the read mode.
fopen function checks technical.txt file in location if it does not exist then it returns NULL otherwise file is opened. In our case let’s assume the file is present so fopen function opens the function.

2. Second fopen function we open file on write mode.
fopen function checks seek.txt file in location if it exists then it open with its contents in it and if the file does not exists then new file with seek.txt name created and a blank file is opened.

Write A Comment