DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More String Errors - String(3C++)

Input and Output

The next group of operators permit input from and output to iostreams. Input into Strings is especially nice because the user need not know the size of the chunk that is being read in. Therefore, the user doesn't have to guess at a buffer size and is in no danger of overflowing buffers when unexpectedly large chunks are input.


<<
ostream& operator<<(ostream&,const String&);

Xout << s sends a copy of String s to ostream Xout and returns Xout.


>>
istream& operator>>(istream&, String&); Xin >> s reads the next (whitespace separated) String from istream Xin and assigns it to String s.

sgets
String sgets(istream&);

sgets(xin) reads characters from istream xin until a new-line is read, or an end-of-file condition is encountered. The new-line character is discarded and the data read is returned as a String.


read
unsigned String::read(istream&, int);

s.read(xin,nbytes) reads at most nbytes characters from istream xin (up to an end-of-file condition) into String s. It returns the number of characters read if successful and -1 otherwise. It destroys the original contents of s either way.

An example of these input/output functions is the following simple program which reads standard input and outputs it to standard output with line numbers added:

   main()
   {
       int count = 0;
       String s;
       while( length(s = sgets(cin)) ) {
           cout << ++count << ":\ t" << s << "\ n";
       }
   }

Next topic: Element Selection and Substrings
Previous topic: Queue-oriented functions

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004