0% found this document useful (0 votes)
3 views8 pages

Stream Classes

The document explains the concept of streams in C++, which are sequences of bytes used for input and output operations. It details the stream class hierarchy, including main classes like istream, ostream, ifstream, and ofstream, and their roles in handling console and file data. Additionally, it outlines how to create and use ifstream and ofstream for reading from and writing to files, respectively.

Uploaded by

Meera Nair
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Stream Classes

The document explains the concept of streams in C++, which are sequences of bytes used for input and output operations. It details the stream class hierarchy, including main classes like istream, ostream, ifstream, and ofstream, and their roles in handling console and file data. Additionally, it outlines how to create and use ifstream and ofstream for reading from and writing to files, respectively.

Uploaded by

Meera Nair
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stream classes in CPP

Streams
• A stream is a sequence of bytes.
• It acts either as a source from which the input
data can be obtained or a destination to which
the output data can be sent.
• The source stream that provides data to the
program is called input [Link] represents the
input stream connected to the standard input
device keyboard.
• The destination stream that receives output from
the program is called output stream represented
by cout connected to the screen by default.
The Stream Class Hierarchy
• The C++ I/O system contains a hierarchy of classes that
are used to define various streams to deal with both
the console and disk files. Theses classes are called the
stream classes.
Main stream classes
• istream is a general purpose input stream. cin is an
example of an istream.
• ostream is a general purpose output stream. cout and
cerr are both examples of ostreams.
• ifstream is an input file stream. It is a special kind of an
istream that reads in data from a data file.
• ofstream is an output file stream. It is a special kind of
ostream that writes data out to a data file.
stream class hierarchy
• ios class is topmost class in the stream classes
hierarchy. It is the base class for istream,
ostream, and streambuf class.
• istream and ostream serves the base classes
for iostream class. The class istream is used
for input and ostream for the output.
• Three classes istream_withassign,
ostream_withassign, iostream_withassign
add assignment operators to theses classes.
• streambuf is another class which provides an
interface to physical devices .
ifstream
• An ifstream is an input file stream, i.e. a stream of data used for reading input
from a file.
• The use of ifstreams ( and ofstreams ) requires the inclusion of the fstream header:
#include <fstream>
• Before you can use an ifstream, however, you must create a variable of type
ifstream and connect it to a particular input [Link] can be done in a single step,
such as:
ifstream fin( "[Link]" );
• Once you have created an ifstream and connected it to an open file, you read data
out of the file the same way that you read from cin:
fin >> xMin;
After you are completely done using a stream,you should always close it to prevent
possible corruption.
• [Link]( );
– After you have closed a stream, you can re-open it connected to a different file if you wish. (
I.e. you can reuse the stream variable. )
The ofstream Class

• An ofstream is an output file stream, and


works exactly like ifstreams, except for output
instead of input.
• Once an ofstream is created, opened, and
checked for no failures, you use it just like
cout:
• ofstream fout( "[Link]" );
fout << minO2 << endl;

You might also like