0% found this document useful (0 votes)
23 views26 pages

5 - Input-Output Streams As An Introduction

The document provides an introduction to I/O streams in C++, explaining the concepts of input and output streams, file handling, and the use of classes and objects. It details how to declare and use input and output streams, the importance of file I/O for permanent data storage, and the formatting of output. Additionally, it covers the concepts of inheritance in stream classes and how derived classes can extend functionality.

Uploaded by

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

5 - Input-Output Streams As An Introduction

The document provides an introduction to I/O streams in C++, explaining the concepts of input and output streams, file handling, and the use of classes and objects. It details how to declare and use input and output streams, the importance of file I/O for permanent data storage, and the formatting of output. Additionally, it covers the concepts of inheritance in stream classes and how derived classes can extend functionality.

Uploaded by

kagorabasoma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

5 - I/O Streams as an Introduction

to Objects and Classes


• 5.1 : Streams and Basic File I/O
• A stream is a flow of characters. If the flow is into your
program, the stream is called an input stream.
• If the flow is out of your program, the stream is called an
output stream
• If the input stream flows from the keyboard, then your
program will take input from the keyboard. If the stream
flows from a file, then your program will take its input
from that file.
• The cin is an input stream connected to the keyboard
and cout is an output stream connected to the screen.
• 5.1.1 Why Use Files for I/O
• The keyboard input and screen output deal with
temporary data.
• File provides means of storing the data permanently.
• Contents of a file remain until a person or program
changes the file.
• An input file can be used over and over again by many
programs without the need to type in the data separately
for each program.
• Files also provide a convenient way to deal with large
quantities of data.
• 5.1.2 File I/O
• When your program takes input from a file it is said to be
reading from the file and when your program sends
output to a file it is said to be writing to the file.
• In C++, a stream is a special kind of variable known as
an object.
• If a stream is to be used to get input from a file, it must
be declared.
• The streams cin and cout are already declared for you.
If a stream is to be connect to a file, it must be declare
just like any other variable.
• The type for input-file stream variables is named
ifstream
• The type for output-file stream variables is named
ofstream.
• Thus, you can declare in_stream to be an input stream
for a file and out_stream to be output stream for another
file as follows
ifstream in_stream;
ofstream out_stream;

• The types ifstream and ofstream are defined in the


library with the header file fstream.h and following
directive should be included
#include <fstream.h>
• Stream variables, such as in_stream and out_stream
declared above, must each be connected to file.
• This is called opening the file and is done with a
function named open.
in_stream.open(“infile.dat”);

• Once you have declared an input stream variable and


connected it to a file using the open function, your
program can take input from the file using the extraction
operator >>.
int one_number, another_number
in_stream >> one_number >> another_number
• An output stream is opened in the same way as what we
just described for input streams. The following declares
the output stream out_stream and connects it to the file
name outfile.dat
ofstream out_stream;
out_stream. open(“outfile.dat”);

• When used with a stream of type ofstream, the member


function open will create the output file if it does not
already exist.
• If the output file does exist, the member function open
will discard the contents of the file, so that the output file
is empty after the call to open.
• After a file is connected to the stream out_stream with a
call to open, the program can send output to that file
using the insertion operator <<.
• e.g.. the following writes two strings and the contents of
the variables one_number and another_number to the
which is connected to the stream out_stream.

out_stream << “one_number = ” << one_number


<< “another_number = ” << another_number;
• Every file should be closed when your program is
finished getting input from the file or sending output to
the file. Closing a file disconnects the stream from the
file.
• A file is closed with a call to the function close.

in_stream.close( );
out_stream.close( );

• The function close takes no arguments.


• // Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat.

#include <fstream.h>
int main()
{
ifstream in_stream;
ofstream out_stream;

int first, second, third;


in_stream >> first >> second >> third;
out_stream << “The sum of the first 3\n”
<< “numbers in infile.dat\n”
<< “is ” << (first + second + third)
<< endl;

in_stream.close( );
out_steam.close( );

return ;
}
• 5.1.3 Introduction to classes and Objects

• The streams in_stream, out_stream, cin and cout are


objects.
• An object is a variable that has functions as well as data
associated with it.
• E.g. the streams in_stream and out_stream both have a
function named open associated with them.
• A function that is associated with an object is called a
member function.
• E.g. open is a member function of the object in_stream
and another function named open is a member of the
object out_stream.
• Suppose you declare the following stream objects:

ifstream in_stream, in_stream2;


ofstream out_stream, out_stream2

• The functions in_stream.open and in_stream2.open are


the same function.
• Similarly out_stream.open and out_stream2.open are the
same function.
• A type whose variables are objects, e.g. ifstream and
ofstream is called a class.
• 5.2 Tools for Stream I/O
• 5.2.1 Formatting Output with Stream
Functions

• The layout of a program’s output is called format of the


output. E.g. the magic formula

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
• If your program is sending output to a file that is
connected to an output stream called out_stream, you
can use these same commands to ensure that numbers
with decimal point will be written in a way we normally
write amounts of money.
• Just insert the following in your program

out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(1);
• The other formatting instructions in our magic formula
are:
• Setf is an abbreviation for set flags. A flag is an
instruction to do something in one of the two possible
ways. If the flag is given as an argument to setf, then the
flag tells the computer to write output to that stream in
some specific way. What it causes the stream to do
depends on the flag.
• In the above example, there are two calls to the function
setf and these two calls set the two flags ios::fixed and
ios::showpoint.
• The flag ios::fixed causes the stream to output numbers
of type double in what is called fixed-point notation.
• If the flag ios::fixed is set, then all floating point numbers
that are output to that stream will be written in ordinary
everyday notation.
• The flag ios::showpoint tells the stream to always include
a decimal point in floating point numbers
• Another useful flag is ios::showpos. If this flag is set for a
stream, then positive numbers output to that stream will
be written with the plus sign in front of them, e.g.
• The width function tells the stream how many spaces to
use when giving an item as output.
cout.setf(ios::showpos);
cout.width(4);
• 5.3 Character I/O
• 5.3.1 The member Functions get and put

• The function get allows your program to read in one


character of input and store it in a variable of type char.
• Every input stream, whether it is an input-file stream or
the stream cin, has get as a member function.
char c1, c2, c3;
cin.get(c1);
cin.get(c2);
cin.get(c3)

Whats the output when AB is typed?


• The function put is analogous to the member function get
except that it is used for output rather than input.
• Put allows your program to output one character.

Syntax:

Output_Stream.put (Char_expression);

Examples:

cout.put(next_symbol;
cout.put(‘a’)
• 5.4 Inheritance
• One of the most powerful features of C++ is the use of
derived classes. A derived class is obtained from
another class by adding features.
• E.g the class of input-file streams is derived from the
class of all input streams by adding additional member
functions such as open and close.
• The cin belongs to the class of all input streams, but
does not belong to the class of input-file streams
because cin has no member functions named open and
close.
• The word inheritance is just another name for the topic of
derived classes.
• 5.1 Inheritance among Stream Classes
• Recall that an object is a variable that has member
functions.
• A class is a type whose variables are objects.
• Streams are objects, so streams types such as ifstream
and ofstream are classes.
• An input-file stream can be connected to a file using the
member function open, but the stream cin has no
member function named open.
• An input-file is a similar but different kind of stream than
cin.
• An input-file stream is of type ifstream.
• cin is of type istream.
• The classes ifstream and istream are different but
closely related types.
• The class ifstream is a derived class of the class istream.
• Consider the following function, which reads two integers
from the input stream source_file and writes their sum to
the screen:
void two_sum(ifstream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 “ + ” << n2 << “ = ” << (n1 + n2) << endl;
}

• Suppose your program contains the above function


definition and the following stream declaration
Ifstream fin;

• If fin is connected to a file with a call to open, you can


use the function two_sum to read two integers from that
file and write their sum to the screen. The call would be
the following

two_sum(fin)

• Suppose that later on in the same program, you want


your program to read two numbers from the keyboard
and write their sum to the screen.
• Since all input streams are similar, you might think you
can use cin as the argument in a second call to two_sum
as shown below:

two_sum(cin); // WILL NOT WORK

• cin is not of type ifstream; cin is of type istream. If you


want to use cin as an argument to a function, then the
corresponding function parameter should be of type
istream.
• The following rewritten version of two_sum will accept
cin as its argument
void better_two_sum(istream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 “ + ” << n2 << “ = ” << (n1 + n2) << endl;
}

• The following function call can be used


better_two_sum(cin);

• The following is also legal


better_two_sum(fin);
• The type ifstream is a derived class of the class istream.
• When we say that some class A is a derived class of
some other class B, it means that class A has all the
features of class B but it also has extra added features.
• E.g. Any stream of type istream can be used with the
extraction operator >>.
• The class ifstream is a derived class of the class istream,
so an object of type ifstream can be used with the
extraction operator >>.
• One added feature is that a stream of type ifstream can
be used with the function open.
• Derived classes are often discussed using the metaphor
of inheritance and family relationships.
• If class B is a derived class of class A, then class B is
called a child of class A and class A is called a parent
of class B.
• The derived class is said to inherit the member
functions of its parent class.
• E.g Every input-file stream inherits the extraction
operator >> from the class of all input streams. This is
why the topic of derived classes is often called
inheritance.

You might also like