Topic :C++ Manipulators: Format Flags | Input Manipulators: | Output Manipulators | Parameterized manipulators:

Stream Manipulations:

Manipulators are Operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display. Up to this Point, the format for inputting or outputting information has been left to the defaults provided by the C++ Input/Output system. However, we can precisely control the format of our data in either of two ways. The first uses Member function of the ios class. the second uses a special type of function called a Manipulator. we will begin by looking at formatting using the ios Member functions.

C++ provides some built-in Manipulators function within some Libraries. we can include these libraries in our program and can use these function:



======Header_file======Brief description
< iostream > Provide basic information required for all stream I/O in C++ by calling various standard functions. in this Module we will discuss how this formatted I/O implemented in C++ by using member functions and stream manipulators
< iomanip >Contains information useful for performing formatted I/O with parameterized stream manipulation.
< fstream > Contains information for user controlled file processing operations.
< strstream > Contains information for performing in-memory formatting or in-core formatting. This resembles file processing, but the I/O operation is performed to and from character arrays rather than files.
< stdiostream > Contains information for program that mixes the C and C++ styles of I/O.


Basic Format Flags:

These Manipulators are usable on both input and output streams, although many only have an effect when applied to either output or input streams.



Independent Flags (Switch ON/OFF):

Independent Flags (Switch ON/OFF):
boolalpha Alphanumerical bool Values:
Sets the boolalpha format flag for the str stream. when the boolalpha format flag is set, bool values are inserted/extracted by their textual representation. either true or false, instead of integral values. this flag can be unset with teh noboolalpha manipulator.
showbaseShow numerical base prefixes.
sets the showbase format flag for the str stream. When the showbase format flag is set, numerical integer values inserted into streams are prefixed with the same prefixes used by C++ literal constants. 0x for hexadecimal values, 0 for actal and no prefix for decimal-base values. This option can be unset with the noshowbase manipulator. when not set, all numerical values are inserted without base prefixes.
showpointshow decimal point:
sets the showpoint format flag for the str stream. when the show point is always written for floating point values inserted into the stream (even for those whose decimal part is zero). This flag can be unset with the noshowpoint manipulator. when not set, the decimal point is only written for numbers whose decimal part is not zero.
ShowposShow positive signs.
Sets the showpos format flag for the str stream. when the showpos format flag is set, a plus sign (+) precedes every non-negative numerical value inserted into the stream. This flag can be unset with the noshowpos manipultors.
Skipws Skip white spaces.
Sets the skipws format flag for the str stream. When the skipws format flag is set, as many white-space characters as necessary are read and discarded from the stream until a non-whitespace character is found before. This applies to every formatted input operation performed with operator >> on stream. This flag can be unset with the noskipws manipulator, forcing extraction operations to consider leading white-space as part of the content to be extracted.
unitbuf Flush buffer after Insertions:
Sets the unitbuf format flag for the str stream. When the unitbuf flag is set, the associated buffer is flushed after each insertion operation. This flag can be unset with the nounitbuf manipulator, not forcing flushes after every insertion.
uppercase Generate upper-case letters.
Sets the uppercase format flag for the str stream. when the uppercase format flag is set, uppercase (capital) letters are used instead of lowercase for representations on output operations involving stream-generated letters, like some hexadecimal representations and numerical base prefixes. This flag can be unset with the nouppercase manipulator, not forcing the use of uppercase for generated letters.


/* Formating Independent flags: InfoBrother:*/
 
#include <iostream>
#include <sstream>//for std::istringstream
#include <ios> //for std::unitbuf
#include <fstream>//for std::ofstream
using namespace std;
 
main()
{
        /* boolalpha & noboolalpha*/
        bool b=true;
        cout<<"boolalpha: "<<boolalpha<<b<<endl;
        cout<<"noboolalpha: "<<noboolalpha<<b<<endl;
 
        /*showbase & noshowbase*/
        int x=15;
        cout<<hex<<"\nshowbase: "<<showbase<<x<<endl;
        cout<<hex<<"noshowbase: "<<noshowbase<<x<<endl;
 
        /*showpoint &noshowpoint*/
        double a = 30;
	double s = 10000.0;
	double pi = 3.1416;
	cout.precision (5);
	cout<<"\nShowpoint: "<<showpoint << a << '\t' 
	                     << s << '\t' << pi << '\n';
 
	cout<<"noshowpoint: "<<noshowpoint << a << '\t' 
	                     << s << '\t' << pi << '\n';
 
	/*Showpos & noshowpos*/
	int p = 1;
	int z = 0;
	int c = -1;
 
	cout<<"\nshowpos: "<<showpos<<p<< '\t' << z 
	                   << '\t' <<c<<endl;
 
	cout<<"noshowpos: "<<noshowpos<<p<< '\t' 
	                   << z << '\t' <<c<<endl;
 
	/*Skipws &  noskipws*/
	char wa, wb, wc;
	istringstream iss (" 123");
	iss>>skipws>>wa>>wb>>wc;
	cout<<"\nSkipws: "<<wa<<wb<<wc<< '\n';
 
	iss.seekg(0);
	iss>>noskipws>>wa>>wb>>wc;
	cout<<"noskipws: "<<wa<<wb<<wc<< '\n';
 
	/*unitbuf & nounitbuf*/
	ofstream outfile ("test.txt");
	outfile<<unitbuf<<"Test "<<"file"<< '\n'; // flushed three times
	outfile.close();
 
	/*uppercase & nouppercase*/
	cout<<showbase<<hex;
	cout<<"\nuppercase: "<<uppercase<<77<< '\n';
	cout <<"nouppercase: "<<nouppercase<<77<< '\n';    
 
	return 0;
}


Numerical base format flags ("basefield" flags):

Numerical base format flags ("basefield" flags):
dec Use decimal base:
Sets the basefield format flag for the str stream to dec. when basefield is set to dec, integer values inserted into the stream are expressed in decimal base ( i.e. radix 10). for input streams, extracted values are also expected to be expressed in decimal base when this flag is set.
hex Use hexadecimal base:
Sets the basefield format flag for the str stream to hex. When basefield is set to hex, integer values inserted into the stream are expressed in hexadecimal base (i.e. radix 16). For input streams, extracted values are also expected to be expressed in hexadecimal base when this flag is set.
oct use Octal base.
Sets the basefield format flag for the str stream to oct. When basefield is set to oct, integer values inserted into the stream are expressed in octal base ( i.e. radix 8) for input streams, extracted values are also expected to be expressed in octal base when this flag is set.


/* Numerical base format flags 
   ("basefield" flags): InfoBrother:*/
 
#include <iostream>
using namespace std;
 
main()
{
	int n = 70;
	cout<<"dec: "<< dec << n << '\n';  //dec
	cout<<"hex: "<< hex << n << '\n';  //hex
	cout<<"oct: "<< oct << n << '\n';  //oct
 
	return 0;
}


Floating-point format flags ("floatfield" flags):

Floating-point format flags ("floatfield" flags):
fixed Use fixed floating-point notation.
Sets the floatfield format flag for the str stream to fixed. when floatfield is set to fixed, floating-point values are written using fixed-point notation. The value is represented with exactly as many digits in the decimal part as specified by the precision field and with no exponent part.
scientific Use scientific floating-point notation.
Sets the floatfield format flag for the str stream to scientific. When floatfield is set to scientific, floating-point values are written using scientific notation. the value is represented always with only one digit before the decimal point, followed by the decimal point and as many decimal digits as the precision field. Finally, this notation always includes an exponential part consisting on the letter e followed by an optional sign and three exponential digits.


/* Floating-point format flags
   ("floatfield" flags): InfoBrother:*/
 
#include <iostream>
using namespace std;
 
main()
{
	double a = 3.1415926534;
	double b = 2006.0;
	double c = 1.0e-10;
 
	cout.precision(5);
 
	cout << "default:\n";
	cout << a << '\n' << b << '\n' << c << '\n';
 
	cout <<"\nfixed:\n"<<fixed;
	cout << a << '\n' << b << '\n' << c << '\n';
 
	cout << "\nscientific:\n" <<scientific;
	cout << a << '\n' << b << '\n' << c << '\n';
 
	return 0;
}


Input Manipulators:

Input Manipulators
ws Extract Whitespaces.
Extracts as many whitespaces characters as possible from the current position in the input sequences. The extraction stops as soon as a non-whitespace character is found. These extracted whitespace character are discarded.


/* Input manipulators: InfoBrother:*/
 
#include <iostream>
#include <sstream> // std::istringstream, std::ws
using namespace std;
 
main()
{
	char a[10], b[10];
 
	istringstream iss ("one \n \t two");
	iss>>noskipws;
	iss>> a >> ws >>b;
	cout<< a <<", "<< b << '\n';
 
	return 0;
}


Output Manipulators:

Output Manipulators:
endlInsert newline and flush.
Its behavior is equivalent to calling ('\n') for character types other than char. and then flush().
endsInsert Null character.
Its behavior is equivalent to calling ( '\0' ) for other character types.
flushFlush stream buffer.
This function requests all characters to be written to the controlled sequence.


/* Output Manipulators: InfoBrother:*/
 
#include <iostream>
#include <fstream>// std::ofstream
using namespace std;
 
main()
{
	//endl;
	int a=100;
	double b=3.14;
	cout<<"endl: "<<endl;
	cout<<a;
	cout<<endl; // manipulator inserted alone
	cout<< b << endl << a*b; // manipulator in concatenated insertion
	endl(cout); // endl called as a regular function
 
 
	//This program requests test.txt to be flushed 100 times.
	ofstream outfile("test.txt");
	for (int n=0; n<100; n++)
	outfile << n <<flush;
	outfile.close();
 
	return 0;
}


Parameterized manipulators:

Parameterized manipulators
setiosflags set format flags.
Sets the format flags specified by parameter mask.
resetiosflagsReset format flags
Unsets the format flags specified by parameter mask.
setbase Set basefield flag.
Sets the base field to one of its possible values, dec, hex, or oct, according to argument base.
setfill Set fill character.
Sets character as the stream's fill character.
setprecision Set decimal precision.
Sets the decimal precision to be used to format floating-point values on output operations.
setwSet field width.
Sets the field width to be used on output operations.


/* Parameterized manipulators: InfoBrother:*/
 
#include <iostream>
#include <iomanip> // std::setiosflags
using namespace std;
 
main()
{
	/* setiosflags */
	cout<<" setiosflags: "<<endl;
	cout<<hex;
	cout<<setiosflags (ios::showbase | ios::uppercase);
	cout<<100<<endl;
 
	/*resetiosflags*/
	cout<<"\nresetiosflags: "<<endl;
	cout<< hex <<setiosflags (ios::showbase);
	cout<<100<<endl;
	cout<<resetiosflags(ios::showbase)<<100<<endl;
 
	/*setbase*/
	cout<<"\nsetbase: "<<endl;
	cout<<setbase(16);
	cout<<110<<endl;
 
	/*setfill*/
	cout<<"\nsetfill: "<<endl;
	cout<<setfill ('x')<<setw (10);
	cout<<77<<endl;
 
	/*setprecision*/
	cout<<"\nsetprecision: "<<endl;
	double f =3.14159;
	cout <<setprecision(9) << f << '\n';
	cout <<fixed;
	cout <<setprecision(9) << f << '\n';
 
	/*setw*/
	cout<<"\nsetw: "<<endl;
	cout <<setw(10);
	cout << 77 <<endl;
 
	return 0;
}






I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement