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 string& p);
istream& operator>>(istream& stream, string& p);
which are defined by the C++ Standard 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. Details about methods
readFile();
readLine();
readString(istream&);
readToDelim(); and
readToken() may be found in the
RWCString description of the
SourcePro API Reference Guide. 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>
#include <fstream>
int main(){
RWCString line;
{ int count = 0;
ifstream istr("testfile.dat");
while (line.readLine(istr)) // Use default value:
// skip whitespace
count++;
std::cout << count << " lines, skipping whitespace.\n";
}
{ int count = 0;
ifstream istr("testfile.dat");
while (line.readLine(istr, false)) // NB: Do not skip
// whitespace
count++;
std::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.