Tools.h++ Manual

104011 Tandem Computers Incorporated 8-5
8
8.2 Simple example
Here's a simple example that exercises
RWbostream
and
RWbistream
through
their respective abstract base classes,
RWvostream
and
RWvistream
:
#include <rw/bstream.h>
#include <rw/cstring.h>
#include <fstream.h>
ifdef_BORLANDC_ //1
# define MODE ios::binary
#else
# define MODE 0
#endif
void save(const RWCString& a, RWvostream& v)
{
v << a; // Save to the virtual output stream
}
RWCString recover(RWvistream& v)
{
RWCString dupe;
v >> dupe; //Restore from the virtual input stream
return dupe;
}
main()
{
RWCString a("A string with\ttabs and a\nnewline.");
{
ofstream f("junk.dat", ios::out|MODE); // 2
RWbostream bostr(f); // 3
save(a, bostr);
} // 4
ifstream f("junk.dat", ios::in|MODE); // 5
RWbistream bistr(f); // 6
RWCString b = recover(bistr); // 7
cout << a << endl; // Compare the two strings // 8