CS201- Introduction to Programming- Lecture 35

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 35 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction to Introduction to ProgrammingProgramming

Lecture 35Lecture 35

Input/Output Input/Output Streams Streams

File File Input/OutputInput/Output

StreaStreamm

scanf ( ) ;scanf ( ) ;printf ( ) ;printf ( ) ;

StreamStream

Stream is an ordered Stream is an ordered

sequence of bytessequence of bytes

Stream Stream Input/OutputInput/Output

Input stream objectInput stream object

cincin

Output stream Output stream objectobject

coutcout

>>>>

<<<<

Stream Operators

int i ;int i ;

char c ;char c ;

cin >> i ;cin >> i ;

cin >> c ;cin >> c ;

ExampleExample

Every stream Every stream has:has:– A sourceA source– A destination A destination

State State

int i , j ;int i , j ;

cin >> i >> j ;cin >> i >> j ;

cout << i / j ;cout << i / j ;

ExampleExample

Formatted Formatted Input / OutputInput / Output

Member FunctionsMember Functions

cin.get ( ) ;cin.get ( ) ;

c = cin.get ( ) ;c = cin.get ( ) ;cin.get ( char cin.get ( char c ) ;c ) ;

cin.read ( char * buffer , streamsize n cin.read ( char * buffer , streamsize n ))

More than one character is read

Integer type to express counts in streams

<<<<

cout.put (char cout.put (char c ) ;c ) ;

#include #include <iostream.h><iostream.h>

iomanip.hiomanip.h

cerrcerrclogclog

Buffered Buffered Input/OutputInput/Output

BufferBuffer

flushflush

““\n”\n”

cout << endl cout << endl ;;

cauxcauxcprncprn

cout << “The value of the first cout << “The value of the first

integer is” << i ; integer is” << i ;

Stream Stream Insertion Insertion OperatorOperator

ostream & ostream :: operator << ( char * ostream & ostream :: operator << ( char * text )text )

Stream Stream Extraction Extraction

OperatorOperator

int i , j ;int i , j ;

cin >> i >> cin >> i >> j ;j ;

ExampleExample

cin.getline ( char * buffer , int buff_size , char delimiter = ‘\cin.getline ( char * buffer , int buff_size , char delimiter = ‘\n’ )n’ )

100

cin.unget cin.unget ( ) ;( ) ;cin.peek ( ) ;cin.peek ( ) ;

cout.put ( char ch ) ;cout.put ( char ch ) ;cout.write ( char * str , int n cout.write ( char * str , int n ) ;) ;

char name [ 60 ] ;char name [ 60 ] ;

cin >> name ;cin >> name ;

cout << name ;cout << name ;

ExampleExample

char name1 [ 30 ] , name2 [ 30 ] ;char name1 [ 30 ] , name2 [ 30 ] ;cin >> name1 >> name2 ;cin >> name1 >> name2 ;

ExampleExample

We learnt We learnt Input / Output StreamInput / Output Stream

cin , cout , cerr , clogcin , cout , cerr , clog How to create our own objectHow to create our own object Overload Stream OperatorsOverload Stream Operators

In Today’s In Today’s Lecture Lecture