CS201- Introduction to Programming- Lecture 35

40
Introduction to Introduction to Programming Programming Lecture 35 Lecture 35

description

Virtual University Course CS201- Introduction to Programming Lecture No 35 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 35

Page 1: CS201- Introduction to Programming- Lecture 35

Introduction to Introduction to ProgrammingProgramming

Lecture 35Lecture 35

Page 2: CS201- Introduction to Programming- Lecture 35

Input/Output Input/Output Streams Streams

Page 3: CS201- Introduction to Programming- Lecture 35

File File Input/OutputInput/Output

Page 4: CS201- Introduction to Programming- Lecture 35

StreaStreamm

Page 5: CS201- Introduction to Programming- Lecture 35

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

Page 6: CS201- Introduction to Programming- Lecture 35

StreamStream

Stream is an ordered Stream is an ordered

sequence of bytessequence of bytes

Page 7: CS201- Introduction to Programming- Lecture 35

Stream Stream Input/OutputInput/Output

Page 8: CS201- Introduction to Programming- Lecture 35

Input stream objectInput stream object

cincin

Output stream Output stream objectobject

coutcout

Page 9: CS201- Introduction to Programming- Lecture 35

>>>>

<<<<

Stream Operators

Page 10: CS201- Introduction to Programming- Lecture 35

int i ;int i ;

char c ;char c ;

cin >> i ;cin >> i ;

cin >> c ;cin >> c ;

ExampleExample

Page 11: CS201- Introduction to Programming- Lecture 35

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

Page 12: CS201- Introduction to Programming- Lecture 35

State State

Page 13: CS201- Introduction to Programming- Lecture 35

int i , j ;int i , j ;

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

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

ExampleExample

Page 14: CS201- Introduction to Programming- Lecture 35

Formatted Formatted Input / OutputInput / Output

Page 15: CS201- Introduction to Programming- Lecture 35

Member FunctionsMember Functions

Page 16: CS201- Introduction to Programming- Lecture 35

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

Page 17: CS201- Introduction to Programming- Lecture 35

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

Page 18: CS201- Introduction to Programming- Lecture 35

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

Page 19: CS201- Introduction to Programming- Lecture 35

<<<<

Page 20: CS201- Introduction to Programming- Lecture 35

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

Page 21: CS201- Introduction to Programming- Lecture 35

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

Page 22: CS201- Introduction to Programming- Lecture 35

iomanip.hiomanip.h

Page 23: CS201- Introduction to Programming- Lecture 35

cerrcerrclogclog

Page 24: CS201- Introduction to Programming- Lecture 35

Buffered Buffered Input/OutputInput/Output

Page 25: CS201- Introduction to Programming- Lecture 35

BufferBuffer

Page 26: CS201- Introduction to Programming- Lecture 35

flushflush

Page 27: CS201- Introduction to Programming- Lecture 35

““\n”\n”

Page 28: CS201- Introduction to Programming- Lecture 35

cout << endl cout << endl ;;

Page 29: CS201- Introduction to Programming- Lecture 35

cauxcauxcprncprn

Page 30: CS201- Introduction to Programming- Lecture 35

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

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

Page 31: CS201- Introduction to Programming- Lecture 35

Stream Stream Insertion Insertion OperatorOperator

Page 32: CS201- Introduction to Programming- Lecture 35

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

Page 33: CS201- Introduction to Programming- Lecture 35

Stream Stream Extraction Extraction

OperatorOperator

Page 34: CS201- Introduction to Programming- Lecture 35

int i , j ;int i , j ;

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

ExampleExample

Page 35: CS201- Introduction to Programming- Lecture 35

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

100

Page 36: CS201- Introduction to Programming- Lecture 35

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

Page 37: CS201- Introduction to Programming- Lecture 35

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

Page 38: CS201- Introduction to Programming- Lecture 35

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

cin >> name ;cin >> name ;

cout << name ;cout << name ;

ExampleExample

Page 39: CS201- Introduction to Programming- Lecture 35

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

ExampleExample

Page 40: CS201- Introduction to Programming- Lecture 35

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