Example
Class RWFile also 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(), SeekTo64() (for large files greater than 2gb), SeekToBegin(), and SeekToEnd(). The details of the RWFile class capabilities are summarized in the SourcePro API Reference Guide.
The following example creates an RWFile object with filename test.dat. If the file is not empty, the code reads an int, increments it, and writes it back to the file. Otherwise, it writes the value 1 to the file.
 
#include <rw/rwfile.h>
#include <iostream>
 
int main ()
{
// Construct the RWFile.
RWFile file ("test.dat");
 
// 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);
if (i)
std::cout << "The value stored in the file is: " << i << std::endl;
else
std::cout << "There was no value stored in the file." << std::endl;
 
++i;
file.SeekToBegin();
file.Write(i); // Rewrite the int.
}
return 0;
}