In this tutorial, We start with new topic of c which is File handling.
File Definition :- File is a collection of information stored on secondary storage(Example :- hard disk drive) for permanent existence.
In c programming, file is a permanent storage where your information is stored.
Files mostly used for permanent storage.
Why we need files
- As in programming language we store variable and array in program but after program ends these variable and array also get vanished as it is non permanent storage medium. But in case of file which is known as permanent storage medium, it contains data even after program exits.
- If we want some data in program and file contain that data then instead of entering data manually you can access data directly from file.
- File makes easy to transfer data from one computer to another.
- File makes handling data easy .
Types of files
There are different type of files which are as follow.
- Text files
- Binary files
Text files :-
These files are the normal text files (.txt) which are easily created by simple notepad or by text editors.
These file contains plain text which are in 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.
File makes easy to mention data as it is in readable format.
Binary files :-
These are another type of file which are stored in permanent storage.
As text files contains plain text , in 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 computer.
These files stores high amount of data.
As data stored in binary which are not readable these files provides high security.
As file contain information we can write ,read ,append or delete information in file .
There are major operation on file which are performed which are given as follow:
- Create a file
- Opening file
- Reading and writing on file
- Closing file
Create file :-
The create operation in file is used to create file on secondary storage.
It creates a new file.
Opening file :-
This operation in file used to open a file.
We can open already exist file in storage otherwise it will create new file .
Reading and writing on 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 file first we need to create the pointer of file given below
This declaration used to allow communicate between program and files.