< C++ Formatted I/O 1 | Main | C++ File I/O 1 >| Site Index | Download >


 

 

 

 

 

MODULE 18a

C++ STREAM FORMATTED I/O 2

-------------------------------------------------------------------------------------------

MODULE 5: C FORMATTED I/O

 

 

 

 

 

 

 

 

My Training Period: xx  hours

 

The source code for this tutorial is available in C++ Formatted I/O source codes.

 

18.4.4  Stream Format States

  • Format state flag specify the kinds of formatting needed during the stream operations.

  • Available member functions used to control the flag setting are: setf(), unsetf() and flags().

  • flags() function must specify a value representing the settings of all the flags.

  • The one argument, setf() function specifies one or more ORed flags and ORs them with the existing flag setting to form a new format state.

  • The setiosflags() parameterized stream manipulator performs the same functions as the setf.

  • The resetiosflags() stream manipulator performs the same functions as the unsetf() member function.  For parameterized stream manipulators you need iomanip header file.

  • Format state flags are defined as an enumeration in class ios. The list for some of the flags is shown below:

Format state flags

Brief description

ios::skipws

Use to skip whitespace on input.

ios::adjustfield

Controlling the padding, left, right or internal.

ios::left

Use left justification.

ios::right

Use right justification.

ios::internal

Left justify the sign, right justify the magnitude.

ios::basefield

Setting the base of the numbers.

ios::dec

Use base 10, decimal.

ios::oct

Use base 8, octal.

ios::hex

Use base 16, hexadecimal.

ios::showbase

Show base indicator on output.

ios::showpoint

Shows trailing decimal point and zeroes.

ios::uppercase

Use uppercase for hexadecimal and scientific notation values.

ios::showpos

Shows the + sign before positive numbers.

ios::floatfield

To set the floating point to scientific notation or fixed format.

ios::scientific

Use scientific notation.

ios::fixed

Use fixed decimal point for floating-point numbers.

ios::unitbuf

Flush all streams after insertion.

ios::stdio

Flush stdout, stderr after insertion.

 

Table 18.6:  State flag format

  • skipws flags indicates that >> should skip whitespace on an input stream.  The default behavior of >> is to skip whitespace.  To change this, use the unsetf(ios::skipws).  ws stream manipulator also can be used for this purpose.

18.4.5  Trailing Zeroes and Decimal Points

// using showpoint, controlling the trailing zeroes and floating points

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

cout<<"Before using the ios::showpoint flag\n"

    <<"------------------------------------"<<endl;

cout<<"cout prints 88.88000 as: "<<88.88000

<<"\ncout prints 88.80000 as: "<<88.80000

<<"\ncout prints 88.00000 as: "<<88.00000

<<"\n\nAfter using the ios::showpoint flag\n"

<<"-----------------------------------"<<endl;

cout.setf(ios::showpoint);

cout<<"cout prints 88.88000 as: "<<88.88000

<<"\ncout prints 88.80000 as: "<<88.80000

<<"\ncout prints 88.00000 as: "<<88.00000<<endl;

}

 

Output:

 

C++ stream formatted I/O showpoint

 

18.4.6  Justification

// using setw(), setiosflags(), resetiosflags() manipulators

// and setf and unsetf member functions

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

   long p = 123456789L;

   // L - literal data type qualifier for long...

   // F - float, UL unsigned integer...

   cout<<"The default for 10 fields is right justified:\n"

          <<setw(10)<<p

          <<"\n\nUsing member function\n"

          <<"---------------------\n"

          <<"\nUsing setf() to set ios::left:\n"<<setw(10);

   cout.setf(ios::left,ios::adjustfield);

   cout<<p<<"\nUsing unsetf() to restore the default:\n";

   cout.unsetf(ios::left);

   cout<<setw(10)<<p

          <<"\n\nUsing parameterized stream manipulators\n"

          <<"---------------------------------------\n"

          <<"\nUse setiosflags() to set the ios::left:\n"

          <<setw(10)<<setiosflags(ios::left)<<p

          <<"\nUsing resetiosflags() to restore the default:\n"

          <<setw(10)<<resetiosflags(ios::left)

          <<p<<endl;

}

 

Output:

 

C++ formatted I/O setw(), setiosflags(), resetiosflags()

// using setw(), setiosflags(), showpos and internal

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

       cout<<setiosflags(ios::internal | ios::showpos)<<setw(12)<<12345<<endl;

}

 

Output:

 

C++ formatted I/O setw(), setiosflags(), showpos internal

18.4.7  Padding

// using fill() member function and setfill() manipulator

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

long p = 30000;

 

cout<<p

       <<" printed using the default pad character\n"

       <<"for right and left justified and as hex\n"

       <<"with internal justification.\n"

       <<"--------------------------------------------\n";

cout.setf(ios::showbase);

cout<<setw(10)<<p<<endl;

cout.setf(ios::left,ios::adjustfield);

cout<<setw(10)<<p<<endl;

cout.setf(ios::internal,ios::adjustfield);

cout<<setw(10)<<hex<<p<<"\n\n";

 

cout<<"Using various padding character"<<endl;

cout<<"-------------------------------"<<endl;

cout.setf(ios::right,ios::adjustfield);

cout.fill('#');

cout<<setw(10)<<dec<<p<<'\n';

cout.setf(ios::left,ios::adjustfield);

cout<<setw(10)<<setfill('$')<<p<<'\n';

cout.setf(ios::internal,ios::adjustfield);

cout<<setw(10)<<setfill('*')<<hex<<p<<endl;

}

 

Output:

 

C++ formatted I/O fill setfill

 

18.4.8  Another Stream Base

// using ios::showbase

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

long p = 2000;

 

cout<<setiosflags(ios::showbase)

    <<"Printing integers by their base:\n"

    <<"--------------------------------\n"

    <<"Decimal    ---> "<<p<<'\n'

    <<"Hexadecimal---> "<<hex<<p<<'\n'

    <<"Octal      ---> "<<oct<<p<<endl;

}

 

Output:

 

C++ formatted I/O ios::showbase

 

18.4.9  Scientific Notation

  • ios::scientific and ios::fixed flags are contained in the static member ios::floatfield  (usage similar to ios::adjustfield and ios::basefield).

  • These flags used to control the output format of floating point numbers.

  • The scientific flag – is set to force the output of a floating point number to display a specific number of digits to the right of the decimal point (specified by the precision member function).

  • cout.setf(0, ios::floatfield) restores the system default format for the floating number output.

  • A program example:

 

// displaying floating number in system default, scientific and fixed format

#include <iostream>

using namespace std;

 

void main(void)

{

double p = 0.000654321, q = 9.8765e3;

 

cout<<"Declared variables\n"

       <<"------------------\n"

       <<"0.000654321"<<'\n'<<"9.8765e3"<<"\n\n";

cout<<"Default format:\n"

       <<"---------------\n"

       <<p<<'\t'<<q<<'\n'<<endl;

cout.setf(ios::scientific,ios::floatfield);

cout<<"Scientific format:\n"

       <<"------------------\n"

       <<p<<'\t'<<q<<'\n';

cout.unsetf(ios::scientific);

cout<<"\nDefault format after unsetf:\n"

       <<"----------------------------\n"

       <<p<<'\t'<<q<<endl;

cout.setf(ios::fixed,ios::floatfield);

cout<<"\nIn fixed format:\n"

       <<"----------------\n"

       <<p<<'\t'<<q<<endl;

}

 

Output:

 

C++ formatted I/O ios::scientific ios::fixed

 

18.4.10  Uppercase and Lowercase

// using ios::uppercase flag

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

long p = 12345678;

 

cout<<setiosflags(ios::uppercase)

       <<"Uppercase letters in scientific\n"

       <<"notation-exponents and hexadecimal values:\n"

       <<"------------------------------------------\n"

       <<5.7654e12<<'\n'

       <<hex<<p<<endl;

}

 

Output:

 

C++ formatted I/O ios::uppercase

// demonstrating the flags() member function any format

// flags() not specified in the argument to flags() are reset.

#include <iostream>

using namespace std;

 

void main(void)

{

    long p = 2000;

    double q = 0.00124345;

   

    // set a new format state

    cout<<"The value of flags variable is: "

       <<cout.flags()<<'\n'

       <<"Print long int and double in original format:\n"

       <<p<<'\t'<<q<<"\n\n";

    

    long OriginalFormat = cout.flags(ios::oct | ios::scientific);

    // save the previous format state

    cout<<"The value of the flags variable is: "

       <<cout.flags()<<'\n'

       <<"Print long int and double in a new format\n"

       <<"specified using the flags member function:\n"

       <<p<<'\t'<<q<<"\n\n";

    

    cout.flags(OriginalFormat);

    // restore the original format setting

    cout<<"The value of the flags variable is: "

       <<cout.flags()<<'\n'

       <<"Print values in original format again:\n"

       <<p<<'\t'<<q<<endl;

}

 

Sample output:

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------------------------------------

 

C++ formatted I/O flags()

 

18.4.11  Stream Error States

cin.eof()

cout.rdstate()

// using eof(), fail(), bad(), good(), clear() and rdstate()

#include <iostream>

using namespace std;

 

void main(void)

{

    int p;

    

    cout<<"Before a bad input operation: \n"

       <<"-----------------------------\n"

       <<"  cin.rdstate(): "<<cin.rdstate()

       <<"\n      cin.eof(): "<<cin.eof()

       <<"\n     cin.fail(): "<<cin.fail()

       <<"\n      cin.bad(): "<<cin.bad()

       <<"\n     cin.good(): "<<cin.good()

       <<"\n\nEnter a character (should be integer): "<<endl;

    cin>>p;

    

    cout<<"After a bad input operation: \n"

       <<"----------------------------\n"

       <<"  cin.rdstate(): "<<cin.rdstate()

       <<"\n      cin.eof(): "<<cin.eof()

       <<"\n     cin.fail(): "<<cin.fail()

       <<"\n      cin.bad(): "<<cin.bad()

       <<"\n     cin.good(): "<<cin.good()<<"\n\n";

    cin.clear();

    cout<<"After cin.clear()\n"

       <<"-----------------\n"

       <<"cin.fail(): "<<cin.fail()<<endl;

}

 

Output:

 

C++ formatted I/O eof(), fail(), bad(), good(), clear()

 

C++ formatted I/O eof(), fail(), bad(), good(), clear()

// displaying floating number in system

// default, scientific and fixed format 

#include <iostream>

using namespace std;

 

void main(void)

{

double p = 0.000654321, q = 9.8765e3;

 

cout<<"Declared variables\n"

     <<"------------------\n"

     <<"0.000654321"<<'\n'<<"9.8765e3"<<"\n\n";

 

cout<<"Default format:\n"

     <<"---------------\n"

     <<p<<'\t'<<q<<'\n'<<endl;

 

cout.setf(ios::scientific,ios::floatfield);

cout<<"Scientific format:\n"

     <<"------------------\n"

     <<p<<'\t'<<q<<'\n';

 

cout.unsetf(ios::scientific);

cout<<"\nDefault format after unsetf:\n"

     <<"----------------------------\n"

     <<p<<'\t'<<q<<endl;

cout.setf(ios::fixed,ios::floatfield);

cout<<"\nIn fixed format:\n"

     <<"----------------\n"

     <<p<<'\t'<<q<<endl;

}

 

Output:

 

C++ formatted I/O Visual C++ .Net

///// -padding.cpp- /////

// using fill() member function and setfill() manipulator

#include <iostream>

#include <iomanip>

using namespace std;

 

int main(void)

{

    long p = 30000;

    

    cout<<p <<" printed using the default character pad\n"

        <<"for right and left justified and as hex\n"

        <<"with internal justification.\n"

        <<"----------------------------------------\n";

    cout.setf(ios::showbase);

    cout<<setw(10)<<p<<endl;

    cout.setf(ios::left, ios::adjustfield);

    cout<<setw(10)<<p<<endl;

    cout.setf(ios::internal, ios::adjustfield);

    cout<<setw(10)<<hex<<p<<"\n\n";

    

    cout<<"Using character padding"<<endl;

    cout<<"-----------------------"<<endl;

    cout.setf(ios::right, ios::adjustfield);

    cout.fill('#');

    cout<<setw(10)<<dec<<p<<'\n';

    cout.setf(ios::left, ios::adjustfield);

    cout<<setw(10)<<setfill('$')<<p<<'\n';

    cout.setf(ios::internal, ios::adjustfield);

    cout<<setw(10)<<setfill('*')<<hex<<p<<endl;

    return 0;

}

 

[bodo@bakawali ~]$ g++ padding.cpp -o padding

[bodo@bakawali ~]$ ./padding

 

30000 printed using the default character pad

for right and left justified and as hex

with internal justification.

----------------------------------------

     30000

30000

0x    7530

 

Using character padding

-----------------------

#####30000

30000$$$$$

0x****7530

 

tenouk fundamental of C++ object oriented tutorial

 

 

 

 

 

 

 

 

 

 

 

Further C++ formatted I/O reading:

 

  1. The source code for this tutorial is available in C++ Formatted I/O source codes.

  2. Check the best selling C / C++, Object Oriented and pattern analysis books at Amazon.com.

  3. Visual C++ .NET programming tutorials.

 

 

 

 

 

 

|< C++ Formatted I/O 1 | Main | C++ File I/O 1 >| Site Index | Download |


C++ Stream Formatted Input/Output:  Part 1 | Part 2