Topic: Concept of Stream: Course Name

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Topic: Concept of Stream

Course Name:
Object Oriented
Programming with
C/C++
Course Code:
19008600

Faculty Name:
Zaiba Khan
Assistant Professor(CSE)
School of
Engineering &
Technology
Introduction
• It is essential to know how to provide the
input data and how to present the results
in a desired form
• C++ supports a rich set of I/O functions &
operations to do this
• C++ supports all of C‟s rich set of I/O
functions
• C++ uses concept of stream & stream
classes to implement its I/O operations
with the console & disk files.
C++ streams
• I/O stream in C++ designed to work with wide
variety of devices. Like
– Terminals
– Disks
– Tape drives
Definition of stream:
• A stream is a sequence of bytes.
• It acts as a source (input data) and as a
destination(output data)
– i.e.
• Data to the program(Input stream)
• Output from the program(Output stream)
C++ streams(Cont….

Stream act as an
interface between the
program and I/O
device.
C++ streams(Cont….
• Therefore , C++ program handles data independent of the
devices used.

• Pre-defined streams are


– Cin
– Cout
– Cerr
• Cin--- standard input stream(connected to keyboard)
• Cout--- standard output stream(connected to screen)
• Cerr--- standard output for error messages(to screen)
– „c‟ means character and „err‟ means error , hence cerr means
“character error”
» cerr << varName; or cerr << “Some String”;
Example: How cerr works?
#include <iostream>
#include <fstream>
int main()
{
char fileName[] = "data.txt";
ifstream infile(fileName);
if(infile)
cout << infile.rdbuf();
else
cerr << "Error while opening the file " << fileName <<endl;
return 0;
}

• When you run the program, the output will be: [if the file could not be
opened]
Error while opening the file data.txt
C++ stream classes
• C++ I/O system contains a hierarchy of classes
that are used to define various streams which
deals with both
– Console
– Disk Files
• These are called stream classes.
Pointer
C++ stream classes(Explanation)
• ios is the base class for istream (input stream) &
ostream(output stream) which are in turn base classes
for iostream(I/O stream)
• ios declared as virtual base class
– Because its member are inherited by the iostream.
• The class istream provides facilities for formatted and
unformatted input.
• The class ostream provides facilities for formatted
output.
• iostream provides the facilities for handling both input
and output streams.
• Three classes---
– istream_withassign
– ostream_withassign
– iostream_withassign
Stream Classes for Console Operations
Class Name Contents
ios •Contains basic facilities that are used by all
(General input/output stream class) other input and output classes
•Also contains a pointer to a buffer
object(streambuf object)
•Declares constants & functions that are
necessary for handling formatted input &
output operations
istream •Inherits the properties of ios
(input stream) •Declares input functions such as get( ),
getline( ) and read( )

ostream •Inherits the properties of ios


(output stream) •Declares output functions put( ) andwrite( )

iostream •Inherits the properties of ios istream and


(input/output stream) ostream through multiple inheritance and
thus contains all the input and output
functions

streambuf •Provides an interface to physical devices


through buffers
•Acts as a base for filebuf class used ios files
Unformatted I/O Operations
• The object cin and cout are used for input and output
of data of various types.
Overloading of << and >>
• >> operator is overloaded in the istream class.
• << operator is overloaded in the ostream class.
• Like:
– Cin>> variable 1>>variable 2>> variable 3……>> variable N are
valid C++ variable names.
– Cout<< item 1 << item 2<<……..<< item N maybe variables or
constants of an basic type.
Unformatted I/O Operations
put() and get() functions
• get() and put() are the member functions of
istream and ostream classes
• Used for single character input/output
operations
• There are two types of get() functions:
– get(char)  assign the input character to its argument.
– get(void)  returns the input character
• char c; cin.get(c) c= cin.get();
– put()  used to output a line of text , character
by character.
• char c; cin.put(“c”) c= cin.put();
Example how get() works
char c;
cin.get(c); // get a character from keyboard
while(c !=„\n‟) //and assign it to c
{
cout<<c; // display the character on screen
cin.get(c); // get another character
}
• This code reads and displays a line of text
(terminated by a new line character)
Example how put() works
• The function put(), a member of ostream class, can
be used to output stream.
• For Example:
– cout.put(„x‟); // Displays x as output
– cout.put(ch); // Displays value of variable ch as output
OR
– A number as an argument can be used to function put()
• cout.put(68); // Displays the character “D” as output
Cont….
• The following segment of a program reads a line of
text:
char c;
cin.get(c); // reads a character
while (c!=„\n‟)
{
cout.put(c); // displays the character on screen
cin.get(c);
}
For Example:
int main ( )
{
Input= OOPs
int count=0;
char c;
cout<< “Input Text\n”; Output= OOPs
cin.get(c); // reads a character Number of characters= 4
while (c!=„\n‟)
{
cout.put(c); // displays the character on screen
count++;
cin.get(c);
}
Cout<< “\nNumber of characters = “<< count << “\n”;
return 0;
}
Unformatted I/O Operations
getline() and write() functions
• We can read and display a line of text more
efficiently by using getline( ) and write ( )
• The getline( ) functions reads a whole line of text
that ends with newline character.

cin.getline(line,size);
• It reads character input & the reading is
terminated as soon as either the newline character
„\n‟ or size-1 characters are read.
char name[20];
cin.getline(name,20);
For Example:
int main ( )
{
int size=20;
char city[20];

cout<< “Enter city name : \n”;


cin >> city;
cout<< “City name : ”<< city << “\n\n”; // waiting to read getline()

cout<< “Enter city name again : \n”; // „\n‟ is read as empty line
cin.getline(city,size);
cout<< “City name now: ”<< city << “\n\n”;

cout<< “Enter another city name : \n”;


cin.getline(city,size);
cout<< “ New City name: ”<< city << “\n\n”;
return 0;
}

You might also like