File Handling
File Handling: – All the programs that we have written so far have extensively used input and output statements i.e. console I/O statements. The scanf() is used to input data from keyboard and printf() to output or display on VDU or screen. Thus, whenever some data is required in a program it tries to read from keyword. The keyword is very slow device. For small amount of data, this method of input works well. But in a program where huge data is needed e.g. a program which generates the merit list of entrance examination may require data about 10000 to 20000 records.
It is not possible to sit down and type such a large amount of data in one go. In another interesting situation, when the data or results produced by one program are required by the program that produced them. Therefore we need to have a mechanism such as files by virtue of which the program can read data from or write data on floppy disk or hard disk. the permanent storage space on a disk is called a file. A data file is a collection of related data stored on the disk.
When the data of a file is stored in the form of readable and printable characters then the file is known as a text file. On the other hand, if a file contains non-readable character in binary code then the file is called as binary file. For instance, the program files created by an editor are stored as text files whereas executed files generated by a compiler are stored as binary files.
Errors During File Operations
When we are doing file operations, there may be a number of errors take place. Some of these errors are given below:
- A file that does not exist, but we are attempting to open it.
- We are creating a file that already exists or opening a file with an invalid filename.
- We are attempting to read past the end of file.
- Device Overflow
- Performing read operations on a file which is opened for write operations or vice versa.
Steps in Processing Files
The Processing of a File involves the following steps:
- Opening a File
- Reading a File Or Writing a File
- Closing a File