Sei sulla pagina 1di 20

MANAGING CONSOLE I/O OPERATIONS

C++ STREAMS C++ STREAM CLASSES

C++ STREAM
The I/O system supply and interface to the programmers i.e there is independence of actual devices being accessed. This interface is known as STREAM. A stream is a sequence of bytes which can be either I/P from a user or a O/P to be displayed.

DATA STREAM
PROGRAM

I/P STREAM

O/P STREAM

Example for managing console I/O operation


Width() :It specify the required size for displaying an output value. Precession() :Specify the number of digits after the decimal point of a float value. Ex:- cout.precession(3); cout<<21.2547889;

Output
21.254 Fill():It is used to fill a char in the unused portion of a field.

Setf():It is used to specify format flags that can control the form of output display. Unsetf():To clear the flags specified.

C++ STREAM CLASSES


I/P
ISTREAM

IOS
STREAM BUF

O/P
OSTREAM

IOSTREAM

ISTREAM_WITH ASSIGN

IOSTREAM_WITH ASSIGN

OSTREAM_WITH ASSIGN

The c++ IO system contains a hierarchy of classes that are used to define various streams to deal with both the console and disc files. These classes are called STREAM CLASSES. IOS is a base class for istream and ostream. The class IOS is declared as the VIRTUAL BASE CLASS. The class istream provides the facilities for formatted and unformatted i/p. The class ostream provides he facilities for formatted o/p.

The three classes namely istream_with assign, iostream_with assign and ostream_with assign add assignment operators to these classes. IOS :-streambuf-> Pointer to a buffer object. Istream:- get(), getline(), read() Ostream:- put(), write() Streambuf:- Provides an interface to physical devices through buffers.

UNFORMATTED I/O OPERATION


Overloaded operators(>> and <<) >> :- Is overloaded in istream. << :- Is overloaded in ostream. Put() and get() :The classes istream and ostream defines 2 member function get() and put() respectively to handle single char i/p and o/p operation.

Get()
Get(char*) Get(void) These are the 2 prototype to fetch a char including blank space, tab and a new line char. Get(char*) :Assigns the i/p char to its argument. Get(void) :Returns the i/p character.

Example
Char c; Cin.get(c); While(c!=\n) { Cout<<c; Cin.get(c); }

Put()
It is used to output a line of text character by character. It is a function of ostream. Syntax:cout.put(char/variable) Example cout.put(total);

Getline()
It reads a whole line of text that ends with a new line character. It is invoked by using the cin object. Syntax:cin.getline(line,size); cin.getline(name,20);

Write()
It is used to display the entire line. It is invoked by using object cout. Syntax:cout.write(line,size); cout.write(name,20);

FORMATTED CONSOLE I/O OPERATION


C++ supports a number of features that to be used for formatting the o/p. These features include IOS CLASS FUNCTION AND FLAGS MANIPULATORS USER DEFINE O/P FUNCTION

IOS FORMAT FUNCTION


WIDTH() PRECESSION() FILL() SETF() UNSETF()

MANIPULATORS
Manipulators are special function that can be included in the i/o statemants to alter the format parameters of a stream. Setw() Setprecession() Setfill() Setiosflags() Resetiosflags() to access these manipulators the file iomanip is included in the program.

DESIGNING OUR OWN MANIPULATORS


We can design our own manipulators for certain special purposes. Syntax:Ostream &manipulator(ostream & output) { -------code return output; }

Example
Ostream &unit(ostream & output) { output<<inches; output.setw(10); return output; } Void main() { Cout<<unit; Return 0; }

THANK YOU
PRESENTED BY V GROUP MITHRA KAVYA THANGAMANI KUMARESAN MANIKANDAN KRISHNAMOORTY LOGESHWARAN MAHAMOOD RIYAS