The Old-Style Iostream Functions

The Format Flags

In the C++ I/O system, each stream has associated with it a set of format flags that control the way information is formatted by a stream. In ios are defined the following enumerated values. These values are used to set or clear the format flags.

adjustfield

basefield

dec

fixed

floatfield

hex

internal

left

oct

right

scientific

showbase

showpoint

showpos

skipws

stdio

unitbuf

uppercase

  

Since these flags are defined within the ios class, you will need to explicitly specify this when using them in a program. For example, to refer to left you will write ios::left.

When the skipws flag is set, leading whitespace characters (spaces, tabs, and newlines) are discarded when performing input on a stream. When skipws is cleared, whitespace characters are not discarded.

When the left flag is set, output is left-justified. When right is set, output is right-justified. When the internal flag is set, a numeric value is padded to fill a field by inserting spaces between any sign or base character. If none of these flags is set, output is right-justified by default.

By default, numeric values are output in decimal. However, it is possible to change the number base. Setting the oct flag causes output to be displayed in octal. Setting the hex flag causes output to be displayed in hexadecimal. To return output to decimal, set the dec flag.

Setting showbase causes the base of numeric values to be shown. For example, if the conversion base is hexadecimal, the value 1F will be displayed as 0x1F.

By default, when scientific notation is displayed, the e is in lowercase. Also, when a hexadecimal value is displayed, the x is in lowercase. When uppercase is set, these characters are displayed in uppercase.

Setting showpos causes a leading plus sign to be displayed before positive values.

Setting showpoint causes a decimal point and trailing zeros to be displayed for all floating-point output—whether needed or not.

By setting the scientific flag, floating-point numeric values are displayed using scientific notation. When fixed is set, floating-point values are displayed using normal notation. When neither flag is set, the compiler chooses an appropriate method.

When unitbuf is set, the buffer is flushed after each insertion operation.

When stdio is set, stdout and stderr are flushed after each output.

Since it is common to refer to the oct, dec, and hex fields, they can be collectively referred to as ios::basefield. Similarly, the left, right, and internal fields can be referred to as ios::adjustfield. Finally, the scientific and fixed fields can be referenced as ios::floatfield.

The format flags are typically stored in a long integer and may be set by various member functions of the ios class.

The I/O Manipulators

In addition to setting or clearing the format flags directly, there is another way in which you may alter the format parameters of a stream. This second way is through the use of special functions called manipulators, which can be included in an I/O expression. The manipulators defined by the old-style iostream library are shown in the following table:

Manipulator

Purpose

Input/Output

dec

Use decimal integers

Input/output

endl

Output a newline character and flush the stream

Output

ends

Output a null

Output

flush

Flush a stream

Output

hex

Use hexadecimal integers

Input/output

oct

Use octal integers

Input/output

resetiosflags (long f)

Turn off the flags specified in f

Input/output

setbase(int base)

Set the number base to base

Output

setfill(int ch)

Set the fill character to ch

Output

setiosflags (long f)

Turn on the flags specified in f

Input/output

setprecision (int p)

Set the number of digits of precision

Output

setw(int w)

Set the field width to w

Output

ws

Skip leading whitespace

Input

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




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net