Vous êtes sur la page 1sur 23

Managing console I/O

Introduction
Every program has input after processing it generates the output. It is essential to know how to provide input and how to present result in desired form. For that cin, cout with operators >> and<< is used. But we should know how to control the desired output.

C++ Streams
C++ uses the concept of streams to implement its I/O operations with the console and disk files. Stream is the interface to the programmer which is independent from the actual device used. Stream is a sequence of bytes. It either acts as source or destination.

Types of streams
Source stream:- from which input data to be obtained. Destination Stream:- that receives output from the program.

Data streams
Input Device Input Stream

Program

Output Device

Output stream

Contt
C++ contains several predefined streams cin represents input stream connected to standard input device. cout represents output stream connected to standard output device.

Stream classes for console I/O


ios

istreamt

ostream

iostream

Class hierarchy

Class name
ios

Contents
Basic facilities used by input,output classes . Declares functions and constants that are necessary for handling I/O.

istream

Inherit properties from ios. Declare input functions get(),getline(), extraction operator>> Inherit properties from ios. Declare input functions put(),write(), extraction operator<<

ostream

iostream

Inherit, isstream, ostream through multiple inheritance

Overloaded operators >> and <<


We have used cin, and cout functions for input and output. This has made possible by overloading operators. >> operator is overloaded in isteam. <<is overloaded in ostream.

put() and get() functions


istream and ostream defines get(), put() functions to handle a single character input/output. get function eg: char c; cin.get(c); put function: cout.put(x); cout.put(x);

getline() and write()


Eg:- char name[20]; cin.getline(name,20);

write line: Syntax:--- cout.write(line,size); cout.write(string2,10);

Formatted console I/O


C++ supports a number of features that could be used to formatting the output. These features include: 1. Ios class functions 2. Manipulators 3. User-defined output functions

ios format functions


functions width() task To specify required field size for displaying output value.

precision()

To specify the number of digits to be displayed after the decimal point of a float value. To specify a character that is fill the unused portion of a field.

fill()

Manipulators
Manipulators Equivalent ios functions

setw()
setprecision() setfill()

width()
precisions() fill()

C++ Manipulators
What is a Manipulator? Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display. There are numerous manipulators available in C++. Some of the more commonly used manipulators are

endl Manipulator: This manipulator has the same functionality as the \n' newline.
cout << "Exforsys" << endl; cout << "Training";

setw Manipulator: This manipulator sets the minimum field width on output. The syntax is: setw(x)
#include<iomanip.h> int main() { clrscr(); int x=9; cout<<setw(6)<<"hi"<<setw(10)<<"hello" <<endl<<setw(4)<<"bye"<<setw(10)<<x<< endl; getch(); return 0; }

setfill Manipulator: This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.

int main() { clrscr(); int x=9; cout<<setw(6)<<"hi"<<setfill(*) <<setw(10)<<"hello" <<endl<<setw(4)<<"bye"<<setw(10) <<x<<endl; getch(); return 0; }

setprecision Manipulator: The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms:
fixed scientific

These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.

void main( ) { float x = 0.1;

cout << fixed << setprecision(3) << x << endl;


cout << scientific << x << endl; } The output of the above example is:

Stream base

Managing output with Manipulators


The header file iomanip provides a set of functions called manipulators, which is used to manipulate the output format.
Manipulators

setw()
setprecision() setfill()

Vous aimerez peut-être aussi