Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

11.2 RWFile

Class RWFile encapsulates the standard C file operations for binary read and write, using the ANSI-C functions fopen(), fwrite(), fread(), etc. This class is patterned on class PFile of the Interviews Class Library (Stanford University, 1987), but has been modernized by Rogue Wave to use const modifiers, and to port to various operating systems. The member function names begin with upper case letters in order to maintain compatibility with class PFile.

11.2.1 Construction

The constructor for class RWFile has the prototype:

This constructor will open or create a binary file called filename with mode set to mode (for example, r+), as defined by the Standard C function fopen(). If mode is zero, which is the default, an existing file will be opened for update (mode r+ for UNIX, rb+ for Windows environments). If filename does not exist, it will be created (mode w+ for UNIX, wb+ for Windows environments). The destructor for this class closes the file.

After constructing an RWFile, you should use member function isValid() to check whether opening the file was successful.

There are member functions to flush the file, and to test whether the file operation has an error, is empty, or at the end-of-file.

11.2.2 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(), SeekToBegin(), and SeekToEnd(). The details of the RWFile class capabilities are summarized in the Essential Tools Module Reference Guide.

The following example creates an RWFile with filename test.dat. The code reads an int (if the file is not empty), increments it, and writes it back to the file:

Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.