www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 31: File Handling Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
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.

NEXT>>>>>Lesson 32. Input File Handling

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.