C++ ios_base fmtflags


Advertisements

Description

This type is used as its parameter and/or return value by the member functions flags, setf and unsetf.

Declaration

Following is the declaration for ios_base::fmtflags.

std::ios_base::fmtflags ff;

The values passed and retrieved by these functions can be any valid combination of the following member constants as shown below −

field member constant effect when set
independent flags boolalpha read/write bool elements as alphabetic strings (true and false).
showbase write integral values preceded by their corresponding numeric base prefix.
showpoint write floating-point values including always the decimal point.
showpos write non-negative numerical values preceded by a plus sign (+).
skipws skip leading whitespaces on certain input operations.
unitbuf flush output after each inserting operation.
uppercase write uppercase letters replacing lowercase letters in certain insertion operations.

numerical base

(basefield)

dec read/write integral values using decimal base format.
hex read/write integral values using hexadecimal base format.
oct read/write integral values using octal base format.

float format

(floatfield)

fixed write floating point values in fixed-point notation.
scientific write floating-point values in scientific notation.

adjustment

(adjustfield)

internal the output is padded to the field width by inserting fill characters at a specified internal point.
left the output is padded to the field width appending fill characters at the end.
right the output is padded to the field width by inserting fill characters at the beginning.

Three additional bitmask constants made of the combination of the values of each of the three groups of selective flags can also be used as shown below.

flag value equivalent to
adjustfield left | right | internal
basefield dec | oct | hex
floatfield scientific | fixed

The values of these constants can be combined into a single fmtflags value using the OR bitwise operator (|).

Example

In below example is shown for ios_base::fmtflags.

#include <iostream>

int main () {

   std::cout.setf (std::ios_base::hex , std::ios_base::basefield);
   std::cout.setf (std::ios_base::showbase);
   std::cout << 100 << '\n';

   std::cout.setf (std::ios::hex , std::ios::basefield);
   std::cout.setf (std::ios::showbase);
   std::cout << 100 << '\n';

   std::cout.setf (std::cout.hex , std::cout.basefield);
   std::cout.setf (std::cout.showbase);
   std::cout << 100 << '\n';

   std::ios_base::fmtflags ff;
   ff = std::cout.flags();
   ff &= ~std::cout.basefield;   
   ff |= std::cout.hex;          
   ff |= std::cout.showbase;     
   std::cout.flags(ff);
   std::cout << 100 << '\n';

   std::cout << std::hex << std::showbase << 100 << '\n';

   return 0;
}

Let us compile and run the above program, this will produce the following result −

0x64
0x64
0x64
0x64
0x64
ios.htm
Advertisements