Friday, December 30, 2011

Input And output (I/O) Stream Flags


Input And output (I/O) Stream Flags
To implement many of the above manipulators, I/O streams have a flag field that specifies the current settings. The flag names and their meanings are given below :



Flag Name
                        Meaning
skipws
right
internal
dec
oct
hex
showbase
showpoint
uppercase
showpos
scientific
fixed
unitbuf
stdio
skip white space during input
left justification of output
pad after sign or base indicator
decimal base (show integers in decimal format)
octal base (show integers in octal format)
hexadecimal base (show integers in hexadecimal format)
show base for octal and hexadecimal numbers
show the decimal point for all floating point numbers
show uppercase hex numbers
show ‘+’ to positive integers
use E for floating notation
use floating notation
flush all stream after insertions
flush stdout,stderr after insertion 

Arguments for setflags

First Argument (Flag Name)
            Second Argument
ios : : skipws

ios : : left
ios : : right
ios : : internal

ios : : adjustfield
ios : : dec
ios : : oct
ios : : hex

ios : : basefield
ios : : showbase
ios : : showpos
ios : : uppercase
ios : : showpoint

ios : : scientific
ios : : fixed

ios : : floatfield
ios : : unitbuf
ios : : stdio

(a)  Turning the bit format flag on   In order to change the state of the cout object, the bits that represent its state must be change. The staf ( ) function is invoked for setting the bit format flags of the I/O stream.
     The general format of the setf ( ) function is,
                    cout.set(flags to be set);
For example,
       cout.staf(ios : : showbase) ;
The bitwise OR ( | ) operator is used in the argument list of the setf( ) function in order to change the bit format flag more than one.

For example,
    cout.setf (ios : : showbase | ios : : showpoint | ios : : uppercase) ;
(b) Turning the bit flag off   The usetf ( ) function is used to change the bits directly off. This function takes exactly one argument to turn off the bit pattern.
           The general syntax of the usetf ( ) function is,
                      Cout.unsetf(flags to be turned off);
           For example,
                      cout.setf (ios : : showbase | ios : : showpoint | ios : : uppercase) ;
(b) Turning the bit format flag off  The usetf ( ) function is used to change the bits directly off. This function takes exactly one argument to turn off the bit pattern.
    The general syntax of the unstf ( ) function is,
              cout.unsetf( flags to be turned off);
  For example,
       cout.unsetf (ios : : uppercase) ;
  The bitwise OR ( | ) operator is used in the argument list in order to turn off more then one bit format flag of the I/O stream.
(c)   Basefield bit format flag   The basefielde format flag is used to display integers in the proper base.
         ios : : dec      show integers in decimal format
         ios : : oct      show integers in octal format
         ios : : hex     show integers in hexadecimal format
   Only one of the above can be set at any time. These format flags control the base in which numbers are displayed. By default, dec is set. The syntax of the basefield bit format flag setting is
      cout.setf (ios : : dec,ios : : basefield) ;
      cout.setf (ios : : oct, ios : : basefield) ;
      cout.setf (ios : : hex, ios : : basefield) ;
  PROGRAM  2.11
A program to display an integer number in different bases, namely, dec, oct, and hex using the basefield format flag setting.
     / /using basefield bit format flag
     #includ <iostream.h>
     void main ( )
     {
         int num = 71 ;
         cout << “ decimal = “ <<num << “\n”;
         cout.setf (ios : : oct, ios : : basefield);
         cout << “ octal = “ << num << “\n”;
         cout.setf (ios : : hex,ios : : basefield);
         cout << “ hexadecimal = “ << num << “\n”;
         cout.setf (ios : : dec, ios : : basefield) ;
         cout << “ decimal = “ << num << “\n”;
     }
Output of the above program
      decimal = 71
      octal = 107
      hexadecimal = 47
      decimal = 71

(d) Showbase bit format flag  The showbase format flag is used to display the base for octal and hexadecimal numbers. If showbase is set, this flag prefaces integral insertions with the base indicators used with C++ constants. If hex is set, for instance, an OX will be inserted in front of any integral insertion. This flag is not set by default. The unsetf ( ) function is invoked to undo the base setting.
The syntax of the showbase flag is,
             cout.setf( ios : : showbase);

PROGRAM 
A program to display the base of the given integer numbers using showbase flag.
    / /using showbase flag
    #include <iostream.h>
    void main ( )
    {
        int num = 71;
        cout.setf (ios : : showbase);
        cout << “ decimal = “ << num << “\n”;
        cout.setf (ios : : oct, ios : : basefield);
        cout << “ octal = “ << num << “\n”;
        cout.setf (ios : : hex, ios : : basefield);
        cout << “ hexadecimal = “ << num << “\n”;
        cout.setf (ios : : dec, ios : : basefield);
        cout << “ decimal = “ << num << “\n”;
    }
 Output of the above program
     decimal = 71
     octal = 0107
     hexadecimal = 0x47
     decimal = 71
 (e) Showpos bit format flag  The showpos format flag is used to display the sign of an integer. If this flag is set, a “+” sign will be inserted before any integral insertion. It remains unset by default. Note that on positive decimal output, the ‘+’ is assumed, but by default it will not appear. If the number is negative, the ‘-‘ sign will always appear. The unsetf ( ) function is invoked undo the setting of a positive sign.
         The syntax of the showpos flag is,
                        cout.setf(ios : : showpos);
       
PROGRAM 
A program to show the sign of the given number using the showpos flag.
     / /using showpos flag
     #include <iostream.h>
     void main ( )
     {
        int num = 71;
        cout.setf (ios : : showbase);
        cout.setf (ios : : showpos);
        cout << “ decimal = “ << num << “\n”;
        cout.setf (ios : : oct, ios : : basefield);
        cout << “ octal = “ << num << “\n”;
        cout.setf (ios : : hex, ios : : basefield);
        cout << “ hexadecimal = “ << num << “\n”;
        cout.setf (ios : : dec, ios : : basefield);
        cout << “ decimal = “ << num << “\n”;
    }
Output of the above program
    decimal = +71
    octal = 0107
    hexadecimal = 0x47
    decimal = +71
(f) Uppercase bit format flag  The uppercase bit format flag is used to display output in uppercase. By default, The following notations always appear in lowercase.
  • A hexadecimal number (a, b, c, d, e, f)
  • The base of a hexadecimal number (0x)
  • A floating point number in the scientific notation (0.3e3)
  The unsetf ( ) function is used to revert back to lowercase. The syntax of the uppercase format flag is,
                   cout.setf (ios : : uppercase);

PROGRAM 
A program to display the base of the given hexadecimal number in uppercase using the flag setting.
     / /using showpos flag
     #include <iostream.h>
     void main ( )
     {
           int num = 0x2abc;
           cout.setf( ios : : uppercase | ios : : showbase);
           cout << “ decimal = “ << num << “\n”;
           cout.setf (ios : : oct, ios : : basefield);
           cout << “ octal = “ << num << “\n”;
           cout.setf (ios : : hex, ios : : basefield);
           cout << “ hexadecimal = “ << num << “\n”;
           cout.setf (ios : : dec, ios : : basefield);
           cout << “ decimal = “ << num << “\n”;
     }
Output of the above program
    decimal = 10940
    octal = 025274
    hexadecimal = 0X2ABC
    decimal = 10940
(g)   Formatting floating point numbers  The following sections explain how floating values are formatted using the different flag setting in C++.

PROGRAM 
A program to show the floating point numbers without any special formatting.
     / /using field justification
     #include <iostream.h>
     void main ( )
     {
          float a, b, c, d ;
          a = 1.23456789 ;
          b = 34.56 ;
          c = 1.34E2 ;
          d = -123.5677 ;
          cout << “a = “ << a << “\n”;
          cout << “b = “ << b << “\n”;
          cout << “c = “ << c << “\n”;
          cout << “d = “ << d << “\n”;
Output of the above program
    a = 1.234568
    b = 34.560001
    c = 134
    d = -123.567703

(h)   Showpoint bit format flag    The showpoint bit format flag is used to show the decimal point for all floating point values. By default, the number of decimal position is six.
        The syntax of the showpoint flag is,
                       cout.setf( ios : : showpoint);
        The unsetf ( ) flag is invoked to undo the showing of all decimal values of a floating point number.

PROGRAM 
A program to display a floating point value with all decimal place.
     / /using showpoint
     #include <iostream.h>
     void main ( )
     {
         float a, b, c, d ;
         a = 1.23456789 ;
         b = 34.56 ;
         c = 1.34E2 ;
         d = -123.5677 ;
         cout.setf (ios : : showpoint);
         cout << “a = “ << a << “\n”;
         cout << “b = “ << b << “\n”;
         cout << “c = “ << c << “\n”;
         cout << “d = “ << d << “\n”;
    }
Output of the above program
     a = 1.234568
     b = 34.560001
     c = 134.000000
     d = -123.567703
(i) Precision   The precision member function is used to display the floating point value as defined by the user. The general syntax of the precision is,
               cout.precision (int n)
where n is number of decimal places of the floating value to be displayed.
     For example,
           cout.precision (5) ;
     which display a floating point number of five decimals.
   
PROGRAM 
A program to display a floating point value in the formatted from using the showpoint and precision member function.
     / /using showpoint and precision member function
     #include <iostream.h>
     void main ( )
     {
          float a, b, c, d;
          a = 1.23456789;
          b = 34.560;
          c = 134.000;
          d = -123.568;
          cout.stef(ios : : showpoint) ;
         cout.precision (3) ;
         cout << “a = “ << a << “\n” ;
         cout << “b = “ << b << “\n” ;
         cout << “c = “ << c << “\n” ;
         cout << “d = “ << d << “\n” ;
     }
Output of the above program
     a = 1.235
     b = 34.560
     c = 134.000
     d = -123.568
(j)  Floatfield bit format flag   Sometimes a floating point value may have to be displayed in scientific notation rather than in fixed format.
(i) Scientific :   When scientific is set, floating point values are inserted using scientific notation. There is only one digit before the decimal point followed by the specified number of precision digits which in turn is followed by an uppercase or a lower case e depending on the setting of uppercase and the exponent value.
    The general syntax of the scientific notation is,
                     Cout.setf( ios : : scientific, ios : : adjustfield);
(ii)  Fixed  When fixed is set, the value is inserted using decimal notation with the specified number of precision digits following the decimal point. If neither scientific nor fixed is set (the default), scientific notation will be used when the exponent is less then 4 or greater than precision. Other wise, fixed notation is used.
     The general syntax of the fixed notation is,
                   cout.setf(ios : : fixed, ios : : adjustfield);

PROGRAM 
A program to display a floating point value in both scientific and fixed notation using the floating format flag.
    / /using fixed and scientific flag
    #include <iostream.h>
   void main ( )
   {
       float a, b, c, d ;
       a = 1.23456789;
       b = 34.56;
       c = 1.35E2;
       d = -123.567 ;
       cout.setf (ios : : showpoint) ;
       cout.precision (3);
       cout.setf (ios : : fixed, ios : : floatfield) ;
       cout << “ display in conventional notation \n”;
       cout << “a = “ << a << “\n” ;
       cout << “b = “ << b << “\n” ;
       cout << “c = “ << c << “\n” ;
       cout << “d = “ << d << “\n” ;
       cout.setf (ios : : scientific, ios : : floatfield) ;
       cout << “ display in scientific notation \n”;
       cout << “a = “ << a << “\n” ;
       cout << “b = “ << b << “\n” ;
       cout << “c = “ << c << “\n” ;
       cout << “d = “ << d << “\n” ;
    }
Output of the above program
     display in conventional notation
     a = 1.235
     b = 34.560
     c = 134.000
     d = -123.568
     display in scientific notation
     a = 1.235e+00
     b = 3.456e+01
     c = 1.340e+02
     d = -1.235e+02

(k) Adjustfield bit format flag   The adjust field consists of there field setting
               ios : : left                   left justification
               ios : : right                 right justification
               ios : : internal            pad after sign or base indicator
(i) Left :   Only one of these may be set at any time. If left is set, the inserted data will be flush left in a field of characters width wide. The extra space, if any, will be filled by the fill character (specified by the fill function).
     The general syntax of the left justification format flag is,
                   cout.setf(ios : : left ,ios : : adjustfield);

(ii) Right : If right is set, the inserted data will be flush right.
     The general syntax of the right justification format flag is,
                     cout.setf(ios::right,ios::adjustfield);

(iii) Internal : If internal is set, the sign of a numeric value will be flush left while the numeric value flush right, and the area between them will contain the pad character.
    The general syntax of the internal justification format flag is,
                    cout.setf(ios : :  interal,ios : : adjustfield);

PROGRAM 
A program to demonstrate how a field justification of an integer value is carried out using the adjust field format flag.
     / /using field justification
     #include <iostream.h>
    void main ( )
    {
        int num = 71;
        cour.fill ( ‘*’ );
        cout.setf (ios : : showpos) ;
        cout.setf (ios : : left, ios : : adjustfield) ;
        cout.width(6) ;
        cout << num << “\n” ;
        cout.setf (ios : : right, ios : : adjustfield) ;
        cout.width(6) ;
        cout << num << “\n” ;
        cout.setf (ios : : internal, ios : : adjustfield) ;
        cout << num << “\n” ;
        cout.width(6) ;
    }
Output of the above program
    +71***
    ***+71
    +***71
(l)  Fill and Width  If the total number of characters needed to display a field is less then the current field width, the extra output spaces will be filled width the current fill character. In C++, it is permitted to use any character to serve as the fill character. But by default it is blank.
(i)  Fill:  The fill ( ) member function is used to specify a new fill character. Once it is specified, it remains as the fill character unless it is changed.
    The general syntax of the fill ( )  member function is,
                  cout.fill(char ch);
    Where ch is a character to be filled.
    For example,
         cout.fill ( ‘*’ ) ;
         cout.fill ( ‘0’ ) ;
(ii) Width :   The width ( ) member function is used to specify the size of the data variable.
       The general syntax of the width ( ) member function is,
                       cout.width(intn);
       where n is a total field width of a variable.
       For example,
           cout.width (10) ;
           cout.width (3) ; 
PROGRAM 
A program to demonstrate how a fill and width member function is used to display a numeral in C++.
        / /using fill and width
        #includ <iostream.h>
       void main ( )
       {  
           cout.width (10) ;
           cout.fill ( ‘x’ ) ;
           int num = 6 ;
           cout << num << “  \n” ;
           cout.width (15) ;
           cout.fill ( ‘b’ ) ;
           num = 12345 ;
           cout << num << endl ;
       }
Output of the above program
    xxxxxxxxx6
    bbbbbbbbbb12345
(m)  Unitbuf   When unitbuf is set, the stream is flushed after every insertion. This flag is unset by default.
    The general syntax of the unitbuf is,
                     cout.setf(ios : : unitbuf);
    / /using unitbuf
    #include <iostream.h>
    void main ( )
    {
        cout.setf (ios : : unitbuf) ;
        cout << “ this is a test program to flush out \n” ;
        cout << “ the input stream\n” ;
    }
(n)  Stdio  This flag flushes the stdout and stderr devices defined in stdio.h This is unset by defult.
     The general syntax of the stdio flag is,
                    cout.setf(ios : : stdio);
    / /using stdio
    #include <iostream.h>
    void main ( )
    {
        cout.setf (ios : : stdio) ;
        cout << “ this is a test program to flush out \n” ;
        cout << “ the input stream\n” ;
    }
(o)   Skipws   If set, leading white space is ignored on extraction. By default skipws is set

No comments:

Post a Comment