DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
iostream examples

Format control

The default treatment of scalar types is that integral values (except char and unsigned char) are inserted in decimal, pointers (except char* and unsigned char*) in hex, floating point types with 6 digits of precision and all without leading or trailing padding. char and unsigned char values are just inserted as single characters. char* and unsigned char* values are treated as pointers to strings (null terminated sequences of characters). The default treatment for extraction of integer types is decimal numbers with leading whitespace permitted. An optional sign (+ or -) is permitted, but without whitespace between it and the digits. Extraction is terminated by a non-digit character. Extraction for floating point types is similar except that the lexical possibilities for floating point numbers are an optional sign followed (without intervening whitespace) by a number according to C++ lexical rules.

For many purposes these defaults are adequate. When they are not, the program can do more formatting itself, or it can use the format control features of the iostream library. The examples in this subtopic use these features.

Associated with each iostream is a collection of ``format state variables'' that control the details of conversions. The most important of these is a long int value that is interpreted as a collection of bits. These bits are declared as:

   enum	{ skipws=01,	// skip whitespace on input
   	  left=02,  right=04, internal=010,
   			// padding location
   	  dec=020, oct=040, hex=0100,
   			// conversion base
   	  showbase=0200, showpoint=0400, uppercase=01000,
   	  showpos=02000,
   			// modifiers
   	  scientific=04000, fixed=010000
   			// floating point notation
   	  } ;
These may be examined and set individually or collectively. For example, the ios::skipws controls whether leading whitespace is skipped by extractors.
   char c ;
   cin.setf(0,ios::skipws) ;	// turn off skipping
   cin >> c ;
   cin.setf(ios::skipws,ios::skipws) ;	// turn it back on


The second argument of setf indicates which bits should be set. The first indicates what values they should be set to.

Manipulators are declared (in iomanip.h) that will have an equivalent effect. The above is equivalent to:

   cin >> resetiosflags(ios::skipws)
       >> c
       >> setiosflags(ios::skipws) ;
resetiosflags resets (makes zero) the indicated bits and setiosflags sets (makes them 1) the indicated bits.

Commonly we want to save the flags (or other state variables) and restore their value later. Consider:

   long f = cin.flags() ;
   cin.setf(ios::skipws,ios::skipws) ;
   cin >> c ;
   cin.flags(f) ;

The variant of flags without an argument returns the current value. state variable The variant with an argument stores the argument into the flags state variable. This code does the same extraction as the previous code, but instead of arbitrarily leaving cin with skipping on it restores skipping to its previous status.

The pattern of member functions is repeated for other state variables. That is, if svar is some state variable, and s is a stream, then s.svar() returns the current value of the state variable and s.svar(x) stores the value x into the state variable.


Next topic: Field widths
Previous topic: Predefined streams

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005