Module 4 C++ Notes
Module 4 C++ Notes
The I/O hierarchy shown below is usually mentioned without the prefix basic_, that is, basic_ ios
is usually referred to as ios. The ios_base class contains the details not needed for templatization.
basic_ios and its descendants are all templatized. basic_streambuf provides the mechanism to
access the stream using lower-level functions. It also provides services to other classes. There are
a few more items in the hierarchy that are not mentioned here.
ios class − This class is the base class for all stream classes. The streams can be input or output
streams. This class defines members that are independent of how the templates of the class are
defined.
• This class contains the necessary facilities that are used by all the other derived classes for
input and output operations.
istream Class − The istream class handles the input stream in c++ programming language.
These input stream objects are used to read and interpret the input as a sequence of characters.
The cin handles the input.
• istream stands for input stream.
• This class is derived from the class ‘ios’.
• This class handle input stream.
• The extraction operator(>>) is overloaded in this class to handle input streams from files to
the program execution.
• This class declares input functions such as get(), getline() and read().
ostream class − The ostream class handles the output stream in c++ programming language.
These output stream objects are used to write data as a sequence of characters on the screen. cout
and puts handle the out streams in c++ programming language.
streambuf class- It is used to get current character and returns the character at the current
position of the controlled input sequence, without modifying the current position.
ifstream class- This class provides input operations. It contains open() function with default
input mode.
• Inherits the functions get(), getline(), read(), seekg() and tellg() functions from the istream.
fstream class- This data type represents the file stream generally, and has the capabilities of
both ofstream and ifstream which means it can create files, write information to files, and read
information from files.
• This class provides support for simultaneous input and output operations.
• Inherits all the functions from istream and ostream classes through iostream.
TEXT FILES
Text files are easier to deal with. A program where a fi le accepts a few lines as input and then
displays those lines one by one. The simplest of functions to be used for this operation.
1. Defining Files
Files can be defined in three possible types.
1. ifstream <filename> (input file stream)
2. ofstream <filename> (output file stream)
3. fstream <filename> (I/O file stream)
The ifstream fi le is a read-only fi le, and one can only read from the fi le defined as ifstream.
The ofstream fi le is an output-only fi le, and one can only write to the fi le defined as ofstream.
The fstream fi le is used for both input and output.
2. Opening Files
Files can be opened using constructors and open functions. Using constructors, one can open a file
with a statement such as
ofstream EntryFile("FewLines.dat")
After defining FewLines.dat to be a physical name for EntryFile logical name, the data written to
the EntryFile will be written to FewLines.dat. The logical name (EntryFile) will vanish when the
program is over, but the physical file name will be there until somebody renames or deletes the
file.
The fi le modes have not been specified. It is assumed to be ios::out (output mode) when the object
of ofstream class (the EntryFile) is defined. Now, take a look at the following statement:
ifstream DisplayFile("FewLines.dat");
4. Closing Files
The function close() is used without any arguments to close a fi le. The allocated fi le handle is
deallocated and the buffers are flushed when a fi le is closed.
BINARY FILES
These fi les are more useful for storing structures of information.
Constructor calls and open functions produce similar effects. Only in the case when the fi le is
already open and there is a need to reopen the fi le with new modes, one may type close() and then
use .open method to open the same fi le in different modes. Note that pipes (|) are used in the
second argument to add multiple nodes.
2. Reading from and Writing to Binary Files
Two member functions for ifstream and ofstream objects are useful in reading and writing.
Both of them have similar syntax. They are:
OfstreamFileObject.write((char *) &<the object>, sizeof(<the same object>))
for writing in the file
IfStreamFileObject.read((char *) &<the object>, sizeof(<the same object>))
for reading from the file.
It is important to understand that the fi le read and write is performed objectwise and not
elementwise. One reads the complete object from the file or writes the complete object to the file
using a single read or write.
Here, both reading the object from the keyboard and writing it to the screen are done elementwise.
3. Closing Binary Files
Closing of binary fi les is similar to closing text fi les. A close() function is needed to close the
binary fi le.
Example for closing a file is
MCA_StudFile_Out.close();