The file is a collection of information which is stored in computer memory File handling in C provides ways to interact with the file.

What is File Handling in C?

File Definition: – File is a collection of information stored on secondary storage(Example :- hard disk drive) for permanent existence.
In c programming, the file is permanent storage where your information is stored.
Files mostly used for permanent storage.
File handling means the process of handling the different operations input/output to file

Why we need files?

As in programming language, we store variable and array in the program but after the program ends these variable and array also get vanished as it is a non-permanent storage medium. But in case of the file which is known as permanent storage medium, it contains data even after program exits.
If we want some data in the program and file to contain that data then instead of entering data manually you can access data directly from a file.

The file makes easy to transfer data from one computer to another.
The file makes handling data easy.

Types of files

There are different types of files which are as follow.

  1. Text files
  2. Binary files

Text files:-

These files are the normal text files (.txt) which are easily created by simple notepad or by text editors.
This file contains plain text which is in a readable format. Whenever we open that file we will see data in file which is stored.
You can easily write, edit , delete data present on file.
The file makes easy to mention data as it is in a readable format.

Binary files:-

These are another type of file which are stored in permanent storage.
As text files contain plain text, in the binary file instead of storing plain text these files stores data in binary format as 0’s and 1’s.
Binary files are mostly the bin files which are stored on the computer.
These files store a high amount of data.
As data stored in binary which are not readable these files provide high security.

File operation

The file contains a set of data. As file contains information we can perform different operations on file such as write, read, append or delete information in file.
There are major operations on file which are performed which are given as follow:

File handling in C
File handling in C
  • Create a file
  • Opening file
  • Reading and writing on file
  • Closing file

Create file:-
The create operation in the file is used to create the file on secondary storage.
It creates a new file.

Opening file:-
This operation is the file used to open a file.
We can open an already existing file in storage otherwise it will create a new file.

Reading and writing on a file:-
Reading operation is used to read the content present in file.
Writing operation is used to write content to file.

Close file:-
After doing operation we need to close the file.
We close the file at the end.

In c, while working with the file first we need to create the pointer of the file given below
FILE *file_pointer;
This declaration used to allow communication between program and files.

Write A Comment