C++ File pointers and Manipulators

C++ File pointers and Manipulators

The header file iomanip provides a set of functions called manipulators which can be used to the manipulate the output formats. They provide the same features as that of the ios(Input output system) member functions and flags. Some manipulators are more convenient to use than their counterparts in the class(Input output system) ios. such that, two or more manipulators can be used as a chain in one statement as shown below:

cout << manip1 << manip2 << manip3 << item;
cout << manip1 << item1 << manip2 << item2;

This kind of concatenation is useful when we want to display several columns of output.The most commonly used manipulators are shown in Table. The table also gives their meaning and equivalents. To access these manipulators, we must include the file iomanip in the program.

Manipulators and their meanings
Manipulator Meaning Equivalent
setw(int w)setprecision(int d) Set the field width ton w. Set the following point precision to d. width()precision()
setfill(intc) Set the fill character to c. fill()
setiosflags(long f) Set he format flag f. setf()
restiosflags(long f) Clear the flag specified by f. unsetf()
Endif Insert new line and flush stream. “\n”

Some examples of manipulators are given below:

cout << setw(10) << 12345;

This statements prints the value 12345 right-justified in a field width of 10 characters. The output can be made left-justified by modifying the statement as follows:

cout << setw(10) << setiosflags(ios : : left) << 12345;

One statement can be used to format output for two or more values. For example, the statement

cout << setw(5) << setprecision(2) << 1.2345
<< setw(10) << setprecision(4) << sqrt(2)
<< setw(15) << setiosflags(ios : : scientific) << sqrt(3);
<< end1;

Will print all the three values in one line with the field sizes of 5,10, and 15 respectively. Note that each output is controlled by different sets of format specifications. We can jointly use the manipulation and the ios functions in a program. The following segment of code is valid:

cout.setf(ios : :showpoint);
cout.setf(ios : :showpos);
cout << setprecsion(4);
cout << setiosflags(ios : : scientific);
cout << setw(10) << 123.45678;

Note! There is a major difference in the way the manipulators are implemented as compared in the ios member functions. The ios member function return the previous format state which can be used later, if necessary. But the manipulator does not return the previous format state. In case, we need to save the old format states, we must use the ios member functions rather than the manipulators. Example:

cout.precsion(2); // previous state
int p = cout.prcision(4); // current state ;

when these statements are executed, p will hold the value of 2(previous state and the new format state will be 4. We can restore the previous format state as follows:
cout.precision(p); // p =2

Program illustrates the following of the output values using both manipulators and ios functions.

FORMATTING WITH MANIPULATORS
#include < iostream >
#include < iomanip >
using namespace std;
int main()
{
cout.setf(ios : : showpoint);
cout << setw(5) << “n”
<< setw(15) << “Inverse_of_ n”
<< setw(15) << “Sum_ of_terms\n\n”;
double term, sum=0;
for(int n=1; n<=10; n++)
{
term = 1.0 / float(n);
sum = sum + term;
cout << setw(5) << n
<< setw(14) << setprecison(4)
<< setiosflags(ios : : scientific) << term
<< setw(13) << resetiosflags(ios : : scientific)
<< sum << end1;
}
return 0;
}

The output of Program 10.8 would be:
n         Inverse_of_n   Sum_of_terms
1       1.0000e+000      1.0000
2       5.0000e-001      1.5000
3       3.3333e-001     1.8333
4       2.5000e-001     2.0833
5       2.0000e-001   2.2833
6       1.6667e-001   2.4500
7       1.4286e-001    2.5929
8       1.2500e-001   2.7179
9       1.1111e-001    2.8290
10       1.0000e-001    2.9290

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise