Tools.h++ Manual
9-2 104011 Tandem Computers Incorporated
9
9.1 Example
Class
RWFile
has member functions to determine the status of a file, and to
read and write a wide variety of built in types, either one at a time, or as
arrays. The file pointer may be repositioned with functions
SeekTo()
,
SeekToBegin()
, and
SeekToEnd()
. The details of the
RWFile
class
capabilities are summarized in Part 2: Class Reference.
For example, to create a
RWFile
with filename "
test.dat
", read an
int
(if the
file is not empty), increment it, and write it back to the file:
#include <rw/rwfile.h>
main()
{
RWFile file("test.dat"); // Construct the RWFile.
// Check that the file exists, and that it has
// read/write permission:
if ( file.Exists() ) {
int i = 0;
// Read the int if the file is not empty:
if ( !file.IsEmpty() ) file.Read(i);
i++;
file.SeekToBegin();
file.Write(i); // Rewrite the int.
}
return 0;
}