Rogue Wave Banner

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

String I/O

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

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.

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 Chapter 6 for details on virtual streams.


Previous file Table of Contents Next file