Home | | Programming and Data Structure II | Standard Libraries

Chapter: Programming and Data structures : C++ Programming Advanced Features

Standard Libraries

A stream is a sequence of bytes (or) conceptually pipe like constructs used for providing I/O.

STANDARD LIBRARIES:

 

Streams and Formatted I/O :

 

·        A stream is a sequence of bytes (or) conceptually pipe like constructs used for providing I/O.

 

·        Streams provide consistent interface for providing independence from having different operations for different IO devices.

 

·        The source stream that provides data to the program is called the input stream.

 

·        The destination stream that receives output from the program is called the output stream.

 

C++ STREAM CLASSES:

 

ios :

 

·        It is the base class for istream and ostream.

 

·        It is declared as the virtual base class.

 

·        It provides the basic support for formatted and unformatted I/O operations.

 

istream:

 

·        Provides facilities for formatted and unformatted input operations.

 

·        Functions such as getc(),getline(),read() are declared.

 

·        Overloaded extraction operator (>>)

 

ostream:

 

·        Provides the facilities for formatted and unformatted output operations.

 

·        Putc() and write() functions are declared.

 

·        Overloaded insertion operator (<<)

 

streambuf:

 

·        It is used to access streams using lower level functions.

 

·        Provides an interface to physical devices through buffers.

 

·        Acts as a base class for filebuf class.

 

iostream:

 

·        Provides facilities for handling both input and output streams.

 

·        Inherits the properties of ios, istream and ostream through multiple inheritance.

 

 

UNFORMATTED I/O OPERATIONS:

 

Overloaded operators >> and << :

 

·        The >> operator is overloaded in the istream.

 

·        The << operator is overloaded in the ostream.

 

cin>>variable 1>>….>>variable n ;

 

The input data are separated by white spaces and should match the type of variable in „cin‟. cout<<variable 1<<….<<variable n;

 

This statement is used for displaying data on the screen.

 

putc() and getc() functions :

 

·        getc() and putc() are used for reading and writing to the streams.

 

·        The getc() function has two different versions : 1. void get(char) 2. char get(void)

 

·        The put() function is used to display the data.

 

·        Get() reads a character at a time from the input stream and put() writes a character at a time to the output stream.

 

getline, read and write functions :

 

·        These functions are used for reading and writing strings.

 

·        The prototypes for these three functions are :

ü   cin.getline(string variable , int size);

 

ü   cin.read(string variable, int size);

 

ü   cin.write(string variable,int size);

 

·     The difference between getline() and read() is that : getline() terminates when a new line is entered but read() does not stop when a new line is encountered.

 

·     read() stops only when end of file (ctrl + z ) is encountered.

 

·     The getline() also stops reading from input if end of file is specified.

 

 

FORMATTED I/O OPERATIONS:

 

1. width() :

ü   it specifies the width for display.

 

ü   it is used in aligning vertical columns.

 

2. precision() :

 

ü   it specifies the precision of the floating point number.

 

ü   default precision is six digits after the decimal

 

point.

 

3. fill() :

 

ü   specifies the character for filling up the unused portion of field.

 

ü   it is used with width().

 

4. setf() :

 

ü  The function specifies format flags that control output display like left (or) right or right justification, padding, scientific notation, displaying base number.

 

5. unsetf():

ü  It provides undo operation for the above mentioned operations.

 

 

MANIPULATORS:

 

·        Manipulators are special functions for formatting.

 

·        The choice between manipulators and ios functions to solve formatting problems sometimes depends on the preference of the user.

 

Manipulators:

ü   setw()

 

ü   setprecison()

 

ü   setfill()

 

ü   setiosflags()

 

ü   resetiosflags()

Characteristics of manipulators:

§    Writing our own manipulators is possible.

 

§    Manipulators are easy to write and produce more readable codes and make the program short.

 

§    They need iostream.h and iomanip.h files.

 

§    When a manipulator does not take any arguments , it is passed without the () parenthesis.

 

§    Some manipulators are needed in pairs to produce the toggle effect.

 

§    They are non-member-functions.

Characteristics of ios functions:

§    They are single and cannot be combined to have multiple effects.

 

§    They need iostream.h file

 

§    ios functions are member functions.

§    The functions do not return the previous status.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : C++ Programming Advanced Features : Standard Libraries |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.