The IString class overloads the input and output operators of the I/O Stream Class Library so that you can extract IString objects from streams and insert IString objects into them. The input operator reads characters from the input stream until a white-space character or EOF is encountered. The IString class also defines a member function to read a single line from an input stream. The following example shows uses of the input and output operators for IString and the lineFrom function:
//Using the IString I/O operators and the lineFrom function
#include <istring.hpp> #include <iostream.h>
void main() {
IString Str1, Str2, Str3;
Str1="Enter some text:";
char test[80];
// Write prompt
cout << Str1;
// Get input
cin >> Str2;
// This only reads in one word of text, so we should
// check to see if this was the only word on the line:
if (cin.peek()!='\n') {
// there's more text on this line so ignore it
cin.ignore(1000,'\n');
}
// Change prompt
Str1.insert("more ",Str1.indexOf(" text:"));
// Write prompt again
cout << Str1;
// Get line of input
Str3=IString::lineFrom(cin,'\n');
// Write output
cout << "First word of first input: " << Str2 << '\n'
<< "Full text of second input: " << Str3 << endl;
}
This example produces the output shown below in regular type, given the input shown in bold:
Enter some text:Here is my first string Enter some more text:Here is my second string First word of first input: Here Full text of second input: Here is my second string
Note that, although null characters are allowed within an IString object, a null character in an input string is treated as the end of the input, and a null character in an IString being written to an output stream ends the output of that IString.