Formatted I/O in C++

C++ helps you to format the I/O operations like determining the number of digits to be displayed after the decimal point, specifying number base etc.

Example:

  • If we want to add + sign as the prefix of out output, we can use the formatting to do so:
    stream.setf(ios::showpos)  
    If input=100, output will be +100 
  • If we want to add trailing zeros in out output to be shown when needed using the formatting:
    stream.setf(ios::showpoint) 
    If input=100.0, output will be 100.000

Note: Here, stream is referred to the streams defined in c++ like cin, cout, cerr, clog.



There are two ways to do so:

  1. Using the ios class or various ios member functions.
  2. Using manipulators(special functions)
  1. Formatting using the ios members:

    The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags.

    Few standard ios class functions are:

    1. width(): The width method is used to set the required field width. The output will be displayed in the given width
    2. precision(): The precision method is used to set the number of the decimal point to a float value
    3. fill(): The fill method is used to set a character to fill in the blank space of a field
    4. setf(): The setf method is used to set various flags for formatting output
    5. unsetf(): The unsetf method is used To remove the flag setting

    Working:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include<bits/stdc++.h>
    using namespace std;
      
    // The width() function defines width 
    // of the next value to be displayed 
    // in the output at the console.
    void IOS_width()
    {
        cout << "--------------------------\n";
        cout << "Implementing ios::width\n\n";
          
        char c = 'A';
           
        // Adjusting width will be 5.
        cout.width(5);
           
        cout << c <<"\n";
           
           
        int temp = 10;
           
        // Width of the next value to be
        // displayed in the output will 
        // not be adjusted to 5 columns.
        cout<<temp;
        cout << "\n--------------------------\n";
    }
      
    void IOS_precision()
    {
        cout << "\n--------------------------\n";
        cout << "Implementing ios::precision\n\n";
        cout << "Implementing ios::width";
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(2);
        cout<<3.1422;
        cout << "\n--------------------------\n";
    }
      
    // The fill() function fills the unused 
    // white spaces in a value (to be printed
    // at the console), with a character of choice.
    void IOS_fill()
    {
        cout << "\n--------------------------\n";
        cout << "Implementing ios::fill\n\n";
        char ch = 'a';
          
        // Calling the fill function to fill 
        // the white spaces in a value with a
        // character our of choice.
        cout.fill('*');
          
        cout.width(10);
        cout<<ch <<"\n";
          
          
        int i = 1;
          
        // Once you call the fill() function, 
        // you don't have to call it again to 
        // fill the white space in a value with
        // the same character.
        cout.width(5);
        cout<<i;
        cout << "\n--------------------------\n";
    }
      
    void IOS_setf()
    {
        cout << "\n--------------------------\n";
        cout << "Implementing ios::setf\n\n";
        int val1=100,val2=200;
        cout.setf(ios::showpos);
        cout<<val1<<" "<<val2;
        cout << "\n--------------------------\n";
    }
      
    void IOS_unsetf()
    {
        cout << "\n--------------------------\n";
        cout << "Implementing ios::unsetf\n\n";
        cout.setf(ios::showpos|ios::showpoint);
        // Clear the showflag flag without 
        // affecting the showpoint flag
        cout.unsetf(ios::showpos); 
        cout<<200.0; 
        cout << "\n--------------------------\n";
    }
      
    // Driver Method
    int main()
    {
        IOS_width();
        IOS_precision;
        IOS_fill();
        IOS_setf();
        IOS_unsetf();
        return 0;

    chevron_right

    
    

    Output:

    --------------------------
    Implementing ios::width
    
        A
    10
    --------------------------
    
    --------------------------
    Implementing ios::fill
    
    *********a
    ****1
    --------------------------
    
    --------------------------
    Implementing ios::setf
    
    +100 +200
    --------------------------
    
    --------------------------
    Implementing ios::unsetf
    
    200.000
    --------------------------
    
  2. Formatting using Manipulators
    The second way you can alter the format parameters of a stream is through the use of special functions called manipulators that can be included in an I/O expression.
    The standard manipulators are shown below:

    1. boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flag
    2. dec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flag
    3. endl: The endl manipulator of stream manipulators in C++ is used to Output a newline character.
    4. and: The and manipulator of stream manipulators in C++ is used to Flush the stream
    5. ends: The ends manipulator of stream manipulators in C++ is used to Output a null
    6. fixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flag
    7. flush: The flush manipulator of stream manipulators in C++ is used to Flush a stream
    8. hex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flag
    9. internal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flag
    10. left: The left manipulator of stream manipulators in C++ is used to Turns on the left flag
    11. noboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flag
    12. noshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flag
    13. noshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flag
    14. noshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flag
    15. noskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flag
    16. nounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flag
    17. nouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flag
    18. oct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flag
    19. resetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in f
    20. right: The right manipulator of stream manipulators in C++ is used to Turns on the right flag
    21. scientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flag
    22. setbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to base
    23. setfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to ch
    24. setiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in f
    25. setprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precision
    26. setw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to w
    27. showbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flag
    28. showpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flag
    29. showpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flag
    30. skipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flag
    31. unitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flag
    32. uppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flag
    33. ws: The ws manipulator of stream manipulators in C++ is used to skip leading white space

    To access manipulators that take parameters (such as setw( )), you must include “iomanip” header file in your program.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iomanip>
    #include <iostream>
    using namespace std;
      
    void Example()
    {
        // performs ssame as setf( )
        cout << setiosflags(ios::showpos);
        cout << 123<<"\n";
          
        // hexadecimal base (i.e., radix 16)
        cout << hex << 100 << endl; 
          
        // Set the field width
        cout << setfill('*') << setw(10) << 2343.0; 
    }
      
    int main()
    {
        Example();
        return 0;
    }

    chevron_right

    
    

    Output:

    +123
    64
    *****+2343
    

    The manipulator setiosflags( ) .



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.