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

Field widths

The default behavior of the inserters is to insert only as many characters as is necessary to represent the value, but frequently programs want to have fixed size fields.

   cout.width(5) ;
   cout << x ;

will output extra space characters preceding the digits to bring the total number of inserted characters to five. If the value of x will not fit in five characters, enough characters will be inserted to express its value. The numeric inserters never truncate. The width state variable might be regarded as an implicit parameter of extractors because it is reset to 0 (which induces the default behavior) whenever it is used.
   cout.width(5) ;
   cout << x << " " << y ;

will output x in at least five characters, but will use only as many characters as necessary in outputting the separating space and y.

The value of the width state variable is honored by the inserters of the iostream library, but user defined inserters are responsible for interpreting it themselves. For example, the Pair inserter defined previously does nothing special with width and so if it is non-zero when the inserter is called the width will apply to the first int inserted, and not the second. If the inserter wants to honor width its definition might look like:

   ostream& operator<<(ostream& o, Pair p) {
   	int w = o.width() ;
   	o.width(w/2) ;
   	o << p.x << " " ;
   	o.width(w/2-((w+1)&1)) ;
   	o << p.y ;
   	return o ;
   	}


This inserts each number in half the requested width.

It is slightly awkward to mix calls to the width() member function with insertion operations. The manipulator setw() may be used. An alternative definition of the Pair inserter might be:

   iostream& operator<<(iostream& ios, Pair p) {
   	int w = ios.width() ;
   	return ios << setw(w/2) << pair.x << " "
   			<< setw(w/2+((w+1)&1)) << pair.y ;
   	}


   Pair
width is always interpreted as a minimum number of characters. There is no direct way to specify a maximum number of characters. In cases where a program wants to insert exactly a certain number of characters, it must do the work itself. For example,
   if ( strlen(s) > w ) cout.write(s,w) ;
   else                 cout << setw(w) << s ;


will always insert exactly w characters.

width is generally ignored by extractors, which tend to rely on the contents of the iostream to detect the end of a field. There is, however, an important exception. The char* extractor interprets a non-zero width to be the size of the array.

For example,

   char a[16] ;
   cin >> setw(sizeof(a)) >> a ;
   if ( !isspace(cin.peek() ) error("string too long") ;


protects the program in case there are sixteen or more visible characters. As a further measure of protection, the extractor stores a trailing null in the last byte of the array when it stops because there are too many visible characters. This means that the number of characters extracted (not counting leading whitespace) will be at most one less than the specified width.

Flags control whether padding (when it occurs) causes the field to be left or right justified. The fill state variable (whose initial value is a space) supplies the character to be inserted.

   cout.fill(*) ;
   cout.setf(ios::left,ios::adjustfield) ;
   cout << setw(5) << 13 << "," ;
   cout.fill(#) ;	// set state variable
   cout.setf(ios::right,ios::adjustfield) ;
   cout << setw(5) << 14 << "\n" ;
results in a line of output that looks like:
   13***,###14

Next topic: Conversion base
Previous topic: Format control

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