C++ file handling
Many real life problems handle large volumes of data and in such situation, we need to use some devices such as floppy disk or harddisk to store the data. The data is stored in these all devices using the concept of file handling or files .
A file is a collection of related data stored in a particular place on the disk
program can be designed to perform the read and write operations on these files
A program typically involves any one or both of the following kinds of data communication
1. Data transfer between the console unit and the program
2. Data transfer between the program and a disk file
We have already discussed in the previous the technique of handling data communication between the console unit and the program. In this chapter we will discuss various methods available
for storing and retrieving the data from files
Then (I/O) Input output operation system of c++ language handles file operations which are very much similar to the console input and output(I/O)
operations. It uses file streams as an interface between the program and the files. The stream that supplies data to the program is known as input stream and another one that receives data from the program is known as output stream.
The input operations involves the creation of an input stream and linking it with the program and the input file. similarly the output operation(OP) involves establishing an output stream with the necessary links with the program and the output file
CLASSES FOR FILE STREAM OPERATIONS
The I/O(Input output) system of C++ programming contains a set of classes that define the file handling methods. These include ifstream, ofstream and fstream. These all classes are derived from fstreambase class and from the corresponding iostream class as show in below figure These classes designed to manage the disk files, are declared in fstream and therefore we must include the file in any program that uses files
Class |
Contents |
filebuf |
it is used to set the file buffers to read and write contains openprot constant used in the open() of file stream classes. Also contain close() and open() as members |
fstreambase |
provides operations common to the file streams, serves as a base for fstrea, ifstream and ofstream class. contains open() and close() functions |
ifstream |
provides input operations. contains open() with default input mode.inherit the functions get(), getline(), read(),seekg() and tellg()
functions from istream |
ofstream |
provides output operations, contains open() with default output mode inherits puts(), seekp(),tellp(), and write() functions from ostream |
fstream |
provides support for simultaneous input and output operations, contains open() with default input mode. Inherits all the functions from istream and ostream classses through iostream
|