Module 4
Module 4
In C++ there are number of stream classes for defining various streams
related with files and for doing input-output operations. All these classes are
defined in the file iostream.h. Figure given below shows the hierarchy of
these classes
1.
ios class is topmost class in the stream classes hierarchy. It is the base class
for istream, ostream, and streambuf class.
2. istream and ostream serves the base classes for iostream class. The
class istream is used for input and ostream for the output.
3. Class ios is indirectly inherited to iostream class using istream and ostream. To
avoid the duplicity of data and member functions of ios class, it is declared as virtual
base class when inheriting in istream and ostream as
2. The istream class: This class is responsible for handling input stream. It provides
number of function for handling chars, strings and objects such as get, getline, read,
ignore, putback etc..
The ostream class: This class is responsible for handling output stream. It provides
number of function for handling chars, strings and objects such as write, put etc..
3. The iostream: This class is responsible for handling both input and output
stream as both istream class and ostream class is inherited into it. It
provides function of both istream class and ostream class for handling
chars, strings and objects such as get, getline, read, ignore, putback,
put, write etc..
File stream
File handling is used to store data permanently in a computer. Using file
handling we can store our data in secondary memory (Hard disk).
How to achieve the File Handling
For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
Streams in C++ :-
We give input to the executing program and the execution program gives back the output.
The sequence of bytes given as input to the executing program and the sequence of bytes
that comes as output from the executing program are called stream. In other words,
streams are nothing but the flow of data in a sequence.
The input and output operation between the executing program and the devices like
keyboard and monitor are known as “console I/O operation”. The input and output
operation between the executing program and files are known as “disk I/O operation”.
C++ Files
The fstream library allows us to work with files.
#include <iostream>
#include <fstream>
There are three classes included in the fstream library, which are used to
create, write or read files:
Class Description
fstream A combination of ofstream and ifstream: creates, reads, and writes to files
Read a File
To read from a file, use either the ifstream or fstream class, and the name of the file.
Opening a Stream
The ifstream and ofstream each have member functions named open which are used to
attaching the stream to a physical filename and opening the file for either reading or
writing. The open member function also provides for a couple of optional arguments that are not
often described. The most general prototype of this function is
It is the responsibility of the programmer to make sure that the buffer is large
enough to hold all the data that is being read
File Pointer
This function accepts no parameters, but returns the location given in bytes from the
beginning of the file where the file pointer is currently sitting. The next read or write
will take place from this location.
1. ios::beg This indicates that the location is the number of bytes from the
beginning of the file.
2. ios::cur This indicates that the location is the number of bytes from the current
file pointer location. This allows for a relative positioning of the file pointer.
3. ios::end This indicates that the location is the number of bytes from the end of
the file.