Introduction to Programming Lecture 35 InputOutput Streams File

  • Slides: 40
Download presentation
Introduction to Programming Lecture 35

Introduction to Programming Lecture 35

Input/Output Streams

Input/Output Streams

File Input/Output

File Input/Output

Stream

Stream

scanf ( ) ; printf ( ) ;

scanf ( ) ; printf ( ) ;

Stream is an ordered sequence of bytes

Stream is an ordered sequence of bytes

Stream Input/Output

Stream Input/Output

Input stream object cin Output stream object cout

Input stream object cin Output stream object cout

Stream Operators >> <<

Stream Operators >> <<

Example int i ; char c ; cin >> i ; cin >> c

Example int i ; char c ; cin >> i ; cin >> c ;

Every stream has: – A source – A destination

Every stream has: – A source – A destination

State

State

Example int i , j ; cin >> i >> j ; cout <<

Example int i , j ; cin >> i >> j ; cout << i / j ;

Formatted Input / Output

Formatted Input / Output

Member Functions

Member Functions

cin. get ( ) ;

cin. get ( ) ;

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

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

cin. read ( char * buffer , streamsize n ) More than one character

cin. read ( char * buffer , streamsize n ) More than one character is read Integer type to express counts in streams

cout. put (char c ) ;

cout. put (char c ) ;

#include <iostream. h>

#include <iostream. h>

iomanip. h

iomanip. h

cerr clog

cerr clog

Buffered Input/Output

Buffered Input/Output

Buffer

Buffer

flush

flush

cout << endl ;

cout << endl ;

caux cprn

caux cprn

cout << “The value of the first integer is” << i ;

cout << “The value of the first integer is” << i ;

Stream Insertion Operator

Stream Insertion Operator

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

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

Stream Extraction Operator

Stream Extraction Operator

Example int i , j ; cin >> i >> j ;

Example int i , j ; cin >> i >> j ;

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

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

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

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

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

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

Example char name [ 60 ] ; cin >> name ; cout << name

Example char name [ 60 ] ; cin >> name ; cout << name ;

Example char name 1 [ 30 ] , name 2 [ 30 ] ;

Example char name 1 [ 30 ] , name 2 [ 30 ] ; cin >> name 1 >> name 2 ;

In Today’s Lecture We learnt n Input / Output Stream cin , cout ,

In Today’s Lecture We learnt n Input / Output Stream cin , cout , cerr , clog n How to create our own object n Overload Stream Operators