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

char* extractor

A useful extractor, but one that must be used with caution, takes a char* second argument. For example,

   char p[100];
   cin >> p;
skips whitespace on cin, extracts visible characters from cin and copies them into p until another whitespace character is encountered. Finally it stores a terminating null (0) character. The char* extractor must be used with caution because if there are too many visible characters in the istream, the array will overflow.

The above example is more carefully written as:

   	char p[100] ;
   	cin.width(sizeof(p)) ;
   	cin >> p ;


There are very few circumstances (perhaps there are none at all) in which it is appropriate to use the char* extractor without setting the ``width'' of the istream.

To make specifying a width more convenient, the setw manipulator (declared in iomanip.h) may be used. The above example is equivalent to:

   	char p[100] ;
   	cin >> setw(sizeof(p)) >> p ;


Next topic: Binary input
Previous topic: User defined extraction operators

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