Tools.h++ Manual

8-6 104011 Tandem Computers Incorporated
8
Program output:
A string with tabs and a
newline.
A string with tabs and a
newline.
The job of function
save(RWCString& a, RWvostream& v)
is to save the
string
a
to the virtual output stream
v
. Function
recover(RWvistream&
)
restores the results. These functions do not know the ultimate format with
which the string will be stored. Some additional comments on particular lines:
1–2 On these lines, a file output stream
f
is created for the file "
junk.dat
".
The default file open mode for the Borland compilers is “text”, requiring
the explicit flag
ios::binary
be used to avoid automatic DOS new line
conversion
1
.
3 On this line, a
RWbostream
is created from
f
.
4 Because this clause in enclosed in braces { ... }, the destructor for
f
will be
called here. This will cause the file to be closed.
5 The file is reopened, this time for input.
6 Now a
RWbistream
is created from it.
7 The string is recovered from the file.
8 Finally, both the original and recovered strings are printed for
comparison.
This program could be simplified by using class
fstream
, which multiply
inherits
ofstream
and
ifstream
, for both output and input. A seek to
beginning-of-file would occur before reading the results back in.
1. With Borland C++, even
ostream::write()
and
istream::read()
perform a text conversion unless
the file is opened with the
ios:: binary
flag.
cout << b << endl;
return 0;
}