Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

3.5 String I/O

Class RWCString offers a rich I/O facility to and from both iostreams and Rogue Wave virtual streams.

3.5.1 iostreams

The standard left-shift and right-shift operators have been overloaded to work with iostreams and RWCStrings:

ostream&operator<<(ostream& stream, const RWCString& cstr);
istream&operator>>(istream& stream, RWCString& cstr);

The semantics parallel the operators:

ostream&operator<<(ostream& stream, const char*);
istream&operator>>(istream& stream, char* p);

which are defined by the Standard C++ Library that comes with your compiler. In other words, the left-shift operator << writes a null-terminated string to the given output stream. The right-shift operator >> reads a single token, delimited by white space, from the input stream into the RWCString, replacing the previous contents.

Other functions allow finer tuning of RWCString input[2]. For instance, function readline() reads strings separated by newlines. It has an optional parameter controlling whether white space is skipped before storing characters. You can see the difference skipping white space makes in the following example:

#include <rw/cstring.h>
#include <iostream.h>
#include <fstream.h>

main(){
   RWCString line;

  { int count = 0;
    ifstream istr("testfile.dat");

    while (line.readLine(istr))             // Use default value:
                                               // skip whitespace
      count++;
    cout << count << " lines, skipping whitespace.\n";
  }
  
  { int count = 0;
    ifstream istr("testfile.dat");
    while (line.readLine(istr, FALSE))        // NB: Do not skip 
                                                    // whitespace
      count++;
    cout << count << " lines, not skipping whitespace.\n";
  }

  return 0;
}

Program Input:

line 1



line 5

Program Output:

2 lines, skipping whitespace.
5 lines, not skipping whitespace.

3.5.2 Virtual Streams

String operators to and from Rogue Wave virtual streams are also supported:

Rwvistream&  operator>>(RWvistream& vstream, RWCString& cstr);
Rwvostream&  operator<<(RWvostream& vstream, 
                        const RWCString& cstr);

By using these operators, you can save and restore a string without knowing its formatting. See Section 6 for details on virtual streams.


Previous fileTop of documentContentsIndexNext file
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.