File Handeling in C
File Handeling in C
of a program. The C language stores all the data available in a program into a file with the help of
file handling in C. This data can be fetched/extracted from these files to work again in any
program.
Table of Contents
What is a File in C?
A file refers to a source in which a program stores the information/data in the form of bytes of
sequence on a disk (permanently). The content available on a file isn’t volatile like the compiler
memory in C. But the program can perform various operations, such as creating, opening, reading
a file, or even manipulating the data present inside the file. This process is known as file handling
in C.
Let us look at a few reasons why file handling makes programming easier for all:
● Reusability: File handling allows us to preserve the information/data generated after we
run the program.
● Saves Time: Some programs might require a large amount of input from their users. In
such cases, file handling allows you to easily access a part of a code using individual
commands.
● Commendable storage capacity: When storing data in files, you can leave behind the
worry of storing all the info in bulk in any program.
● Portability: The contents available in any file can be transferred to another one without
any data loss in the computer system. This saves a lot of effort and minimises the risk of
flawed coding.
● Text Files
● Binary Files
Text Files
The text files are the most basic/simplest types of files that a user can create in a C program. We
create the text files using an extension .txt with the help of a simple text editor. In general, we can
use notepads for the creation of .txt files. These files store info internally in ASCII character
format, but when we open these files, the content/text opens in a human-readable form.
Text files are, thus, very easy to access as well as use. But there’s one major disadvantage; it
lacks security. Since a .txt file can be accessed easily, information isn’t very secure in it. Added to
this, text files consume a very large space in storage.
To solve these problems, we have a different type of file in C programs, known as binary files.
Binary Files
The binary files store info and data in the binary format of 0’s and 1’s (the binary number system).
Thus, the files occupy comparatively lesser space in the storage. In simpler words, the binary
files store data and info the same way a computer holds the info in its memory. Thus, it can be
accessed very easily as compared to a text file.
The binary files are created with the extension .bin in a program, and it overcomes the drawback
of the text files in a program since humans can’t read it; only machines can. Thus, the
information becomes much more secure. Thus, binary files are safest in terms of storing data
files in a C program.
used to set the file pointer to the intended file position fseek()
Note: It is important to know that we must declare a file-type pointer when we are working with
various files in a program. This helps establish direct communication between a program and the
files.
FILE *fpointer;
Out of all the operations/functions mentioned above, let us discuss some of the basic operations
that we perform in the C language.
● Here, if we suppose that the file – recentprogram.txt doesn’t really exist in the
E:\\myprogram location. Here, we have used the mode “w”. Thus, the first function will
create a new file with the name recentprogram.txt and then open it for writing (since we
have used the “w” mode).
● The “w” here refers to writing mode. It allows a programmer to overwrite/edit and create
the contents in a program file.
● Now, let us take a look at the second binary previousprogram.bin file that is present in the
E:\\myprogram location. Thus, the second function here will open the file (that already
exists) for reading in the “rb” binary mode.
● The “rb” refers to the reading mode. It only allows you to read a file, but not overwrite it.
Thus, it will only read this available file in the program.
Let us take a look at a few more opening modes used in the C programs:
rb Open a file for reading the content In case the file doesn’t exist in
in binary mode. the location, then fopen() will
return NULL.
w Open a file for writing the content. In case the file exists, its
contents are overwritten.
wb Open a file for writing the content in In case the file exists, then its
binary mode. contents will get overwritten.
a Open a file for appending the In case the file doesn’t exist in
content. the location, then it will create
a new file.
Meaning, the data of the program is
added to the file’s end in a program.
ab Open a file for appending the In case the file doesn’t exist in
content in binary mode. the location, then it will create
a new file.
Meaning, the data of the program is
added to the file’s end in a program
in a binary mode.
r+ Open a file for both writing and In case the file doesn’t exist in
reading the content. the location, then fopen() will
return NULL.
rb+ Open a file for both writing and In case the file doesn’t exist in
reading the content in binary mode. the location, then fopen() will
return NULL.
w+ Open a file for both writing and In case the file exists, its
reading. contents are overwritten.
wb+ Open a file for both writing and In case the file exists, its
reading the content in binary mode. contents are overwritten.
a+ Open a file for both appending and In case the file doesn’t exist in
reading the content. the location, then it will create
a new file.
ab+ Open a file for both appending and In case the file doesn’t exist in
reading the content in binary mode. the location, then it will create
a new file.
fclose(fptr);
In this case, the fptr refers to the file pointer that is associated with that file that needs to be
closed in a program.
#include <stdlib.h>
int main()
int val;
FILE *fptr;
// if you are using Linux or MacOS, then you must use appropriate locations
if(fptr == NULL)
exit(1);
scanf(“%d”,&val);
fprintf(fptr,”%d”,val);
fclose(fptr);
return 0;
The program mentioned here would take the number given by then store it in the
currentprogram.txt file.
Once we compile this program and run it on the system, we will be able to witness a
currentprogram.txt text file created in the C drive of our computer! Also, when we open this file,
then we will be able to see what integer we entered as an input while coding.
#include <stdlib.h>
int main()
int val;
FILE *fptr;
// The program will exit in case the file pointer fptr returns NULL.
exit(1);
fscanf(fptr,”%d”, &val);
fclose(fptr);
return 0;
The program given here would read the integer that we have input in the currentprogram.txt file
and then print the integer as an output on the computer screen.
In case you were successful in creating the file with the help of Example 1, then the running of
the program in Example 2 will generate the integer that you have input.
You can also use other functions such as fputc(), fgetchar(), etc., in a similar manner in the
program.
How do we Read and Write the Data to the Binary File in a Program?
We have to use the functions fwrite() and fread() for writing to and reading from a file present on
a disk respectively, for the binary files.
Practise Problems
1. Take a look at the following program:
#include <stdio.h>
int main()
file *file;
if(file=fopen(“programtest.txt”,”w”));
fclose(file);
return 0;
A. math.h
B. file.h
C. canio.h
D. stdio.h
Answer – D. stdio.h
File handling refers to the method of storing data in the C program in the form of an output or
input that might have been generated while running a C program in a data file, i.e., a binary file or
a text file for future analysis and reference in that very program.
We can use a variety of functions in order to open a file, read it, write more data, create a new file,
close or delete a file, search for a file, etc. These are known as file handling operators in C.
We create the text files using an extension .txt with the help of a simple text editor. The binary
files store info and data in the binary format of 0’s and 1’s (the binary number system). The binary
files are created with the extension .bin in a program, and it overcomes the drawback of the text
files in a program.