HowToProgramC : Lesson 31

Back Home Next

File Handling

We will discuss files and file handling in this lesson. The topic is going to be a sequel of the subjects like bit, bytes, character, numbers etc.

In the previous lessons, we have talked about strings, which are actually character arrays. While typing a letter or a document in word processor, we actually deal with big collection of words like sentences, not with bits and bytes. These combinations of characters, words, sentences and paragraph are called as files.

The files in the computer are classified under different categories. Primarily, there are two types of files i.e.

  • Data files
  • Executable program files
Data files include our simple text files, or word processor file etc. On the other hand, the executable program files run the program.

Text file Handling

Let's see what are the basic steps needed for file handling. Suppose we have a file on the disk and want to open it. Then read from or write into the file before finally closing it. The basic steps of file handling are:

  • Open the file
  • Read and write
  • Close the file

We have been using cin and cout a lot in the programs. We know that these are the doors by which data can enter and come out. cin is used to enter the data and cout is used to display the data on the screen.

Technically, these are known as streams in C++. This is how 'C++ language' handles files. For this purpose, the header file to be used is <fstream.h> (i.e. file stream). Whenever using files in the program, we will include this header file as

#include <fstream.h>

These streams are used the way we have been employing cin and cout but we can do more with these streams.

While handling files, one can have three options. Firstly, we will only read the file. It means the file is used as input for the program. We need to have a stream for input file i.e. ifstream (input file stream).

Similarly, if we want to write in some file, ofstream (output file stream) can be used. Sometimes we may need to read and write in the same file. One way is to read from a file, manipulate it and write it in another file, delete the original file and renaming the new file with the deleted file name. We can read, write and manipulate the same file using fstream.h.

Let's us see how can we use these files in our programs. First, we have to include the fstream.h in our programs. Then we need to declare file streams. cin and cout are predefined streams which needed not to be declared. We can declare file stream as:

ifstream inFile; // object for reading from a file
ofstream outFile; // object for writing to a file

The variables inFile and outFile are used as 'handle to refer' files. These are like internal variables, used to handle the files that are on the disk. We will use inFile as declared above to read a file. Any meaningful and self-explanatory name can be used. To deal with a payroll system, payrollDataFile can be used as a file stream variable i.e.

ifstream payrollDataFile;.

The topic of file handling will continue in the following lessons.

Back Home Next

© All rights Reserved.