Data File Handling in C
File is a collection of logically related data. It is the way to store data permanently on computer so that we can retrieve and use it later. A file can also be defined as a collection of bytes stored on some storage device like hard disk, CD, DVD etc.
Why do we need file Handling?
The input and output operation are generally through monitor screen and keyboard only. After the termination of program all the entered data is lost because memory is volatile.
If we want to use the data later, then it becomes necessary to keep it in permanent storage device. c language provides the concept of file through which data can be stored on a disk or some other secondary storage device. The stored data can be read whenever required.
Types of File Handling
The file handling can be categorized in two types:
High level (Standard files or stream oriented files):
High level file handling is managed by library functions of C language. High level file handling is most commonly used because it is easier to use and hide most of the details from the programmer.
Low level (system oriented files):
Low level files handling is managed by system calls.
Ways of storing data in files
There are two ways of storing data in files.
1. Text Format
A text file stores the data in form of ASCII characters. In text format, data is stored as a line of characters with each line terminated by a new line character (‘\n’). Text files are in human readable form. So they can be created and read using any text editor.
The speed of processing text files is slow as compared to binary files because some internal translations take place while reading or writing data from file.
2. Binary Format
In binary format, data is stored on the disk in the same way as it is represented in the computer memory. Binary files are not in human readable form. They can be created and read by a specific program written for them .The binary data stored in the file can’t be read by any editor.
The input and output operation in binary files take less time as compared to that of the text files because in binary files no conversion has to take place. However the data written using binary format is not very portable since the size of data types and byte order may be different on different machines. In text format, these problems do not arise. So it is more portable.