Coupon Accepted Successfully!


File Stream Classes

A stream is nothing but a flow of data. In the object-oriented programming, the streams are controlled using the classes. The operations with the files mainly consist of two types. They are read and write. C++ provides various classes, as shown in Figure, to perform these operations. Theios class is the base class. All other classes are derived from the ios class. These classes contain several member functions that perform input and output operations. The streambuf class has low-level routines for controlling data buffer.

Fig: iostream class summary
The istream and ostream classes control input and output functions, respectively. The ios is the base class of these two classes. The member functions of these classes handle formatted and unformatted operations (for more details about formatted and unformatted functions, please refer Chapter 3) input and output in C++ (section 3.6). The functions get(), getline(), and read() and overloaded extraction operators (>>) are defined in the istream class. The functions put(), write(), and overloaded insertion operators (<<)are defined in the ostreamclass.

iostream class is also a derived class. It is derived from istream and ostream classes. There are three other useful derived classes. They are istream_withassign, ostream_withassign, and istream_withassign. They are derived from istream,ostream, and iostream, respectively.
The classes ifstream and ofstream are derived from istream and ostream, respectively. These classes handle input and output with the disk files. The header file fstream.h contains a declaration of ifstream, ofstream, and fstream classes, including isotream.h file. This file should be included in the program while doing disk I/O operations.
The filebuf: filebuf accomplishes input and output operations with files. The streambuf class does not organize streams for input or output operations. The derived classes of streambuf perform these operations. It also arranges a space for keeping input data and for sending output. The I/O functions of the classes istream and ostream invoke the filebuf functions to perform the insertion or extraction on the streams. It holds constant openprot used in function open() and close() as a member.
The fstreambase: The fstreambase acts as a base class for fstream, ifstream, and ofstream. The functions such as open() and close() are defined in fstreambase.
The ifstream: This class is derived from fstreambase and istream by multiple inheritance. It can access the member functions such as get(), getline(), seekg(), tellg(), and read(). It allows input operations and provides the open() function with the default input mode.
The ofstream: This class is derived from fstreambase and ostream classes. It can access the member functions such as put(), seekp(), write(), and tellp(). It allows output operations and provides the member function open() with the default output mode.
The fstream: It allows simultaneous input and output operations on a filebuf. The member function of the base classes istream and ostream starts the input and output. For example, fstream invokes the member function istream::getline() to read characters from the file. It provides the open() function with the default input mode.

Test Your Skills Now!
Take a Quiz now
Reviewer Name