Files are the collection of related data that a computer treats as a single unit. Computers store files to the secondary storage device so that the contents of files remain intact when a computer shuts down and can be obtained anytime we require those contents.
When a computer reads a file, it copies the file from the storage device to memory. When it writes to a file, it transfers data from memory to the storage device. In order to use the file, we have to learn about File I/O i.e. how to read from a file and how to write in a file with a C program.
C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
Steps to follow for File Processing
Create the File Pointer to access each file used. The file pointer is a pointer variable using the FILE structure
as: FILE *p;
where p is the name of the file pointer.
Open the file, associating the pointer name with the file name.
Read and/or write the data from the file.
Close the file after use.
Why files are needed?
When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to enter them all. However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C.
You can easily move your data from one computer to another without any changes.