#include <rw/rwfile.h> RWFile f("filename");
Class RWFile encapsulates binary file operations using the Standard C stream library (functions fopen(), fread(), fwrite(), etc.). This class is based on class PFile of the Interviews Class Library (1987, Stanford University). The member function names begin with upper case letters in order to maintain compatibility with class PFile .
Because this class is intended to encapsulate binary operations, it is important that it be opened using a binary mode. This is particularly important under MS-DOS -- otherwise bytes that happen to match a newline will be expanded to (carriage return, line feed).
If a function expects an RWFile to exist, your code should always check isValid() prior to calling that function. Failure to do so may cause a runtime error. For example:
RWFile aFile("filename"); //... if(aFile.isValid() && ! aFile.Error()){ //... }
None
RWFile(const char* filename, const char* mode = 0);
Constructs an RWFile to be used with the file of name filename and with mode mode. The mode is as given by the Standard C library function fopen(). If mode is zero (the default) then the constructor will attempt to open an existing file with the given filename for update (mode "rb+"). If this is not possible, then it will attempt to create a new file with the given filename (mode "wb+"). The resultant object should be checked for validity using function isValid(). Note that Windows users may be required to call AnsiToOEM on a Windows character constant for the filename in this constructor, because we use a C function call that may resolve to a DOS system call. In that case, DOS might not be able to recognize Windows character sets.
~RWFile();
Performs any pending I/O operations and closes the file.
const char* Access();
Returns the access mode with which the underlying FILE* was opened.
void ClearErr();
Reset error state so that neither Eof() nor Error() returns TRUE. Calls C library function clearerr().
RWoffset CurOffset();
Returns the current position, in bytes from the start of the file, of the file pointer.
RWBoolean Eof();
Returns TRUE if an end-of-file has been encountered.
RWBoolean Erase();
Erases the contents but does not close the file. Returns TRUE if the operation was successful.
RWBoolean Error();
Returns TRUE if a file I/O error has occurred as determined by a call to the C library function ferror(). Before calling Error() you should call isValid to determine if the file was opened/created successfully. Failure to do so could result in runtime errors.
RWBoolean Exists();
Returns TRUE if the file exists.
RWBoolean Flush();
Perform any pending I/O operations. Returns TRUE if successful.
const char* GetName();
Returns the file name.
FILE* GetStream();
Returns the FILE* that underlies the RWFile interface. Provided for users who need to "get under the hood" for system-dependent inquiries, etc. Do not use to alter the state of the file!
RWBoolean IsEmpty();
Returns TRUE if the file contains no data, FALSE otherwise.
RWBoolean isValid() const;
Returns TRUE if the file was successfully opened, FALSE otherwise.
RWBoolean Read(char& c); RWBoolean Read(wchar_t& wc); RWBoolean Read(short& i); RWBoolean Read(int& i); RWBoolean Read(long& i); RWBoolean Read(unsigned char& c); RWBoolean Read(unsigned short& i); RWBoolean Read(unsigned int& i); RWBoolean Read(unsigned long& i); RWBoolean Read(float& f); RWBoolean Read(double& d);
Reads the indicated built-in type. Returns TRUE if the read is successful. If you are already at the end of a file, this will be interpreted as successfully reading a null string, and will return TRUE.
RWBoolean Read(char* i, size_t count); RWBoolean Read(wchar_t* i, size_t count); RWBoolean Read(short* i, size_t count); RWBoolean Read(int* i, size_t count); RWBoolean Read(long* i, size_t count); RWBoolean Read(unsigned char* i, size_t count); RWBoolean Read(unsigned short* i,size_t count); RWBoolean Read(unsigned int* i, size_t count); RWBoolean Read(unsigned long* i, size_t count); RWBoolean Read(float* i, size_t count); RWBoolean Read(double* i, size_t count);
Reads count instances of the indicated built-in type into a block pointed to by i. Returns TRUE if the read is successful. Note that you are responsible for declaring i and for allocating the necessary storage before calling this function.
RWBoolean Read(char* string);
Reads a character string, including the terminating null character, into a block pointed to by string. Returns TRUE if the read is successful. Note that you are responsible for declaring string and for allocating the necessary storage before calling this function. Beware of overflow when using this function.
RWBoolean SeekTo(RWoffset offset);
Repositions the file pointer to offset bytes from the start of the file. Returns TRUE if the operation is successful.
RWBoolean SeekToBegin();
Repositions the file pointer to the start of the file. Returns TRUE if the operation is successful.
RWBoolean SeekToEnd();
Repositions the file pointer to the end of the file. Returns TRUE if the operation is successful.
RWBoolean Write(char i); RWBoolean Write(wchar_t i); RWBoolean Write(short i); RWBoolean Write(int i); RWBoolean Write(long i); RWBoolean Write(unsigned char i); RWBoolean Write(unsigned short i); RWBoolean Write(unsigned int i); RWBoolean Write(unsigned long i); RWBoolean Write(float f); RWBoolean Write(double d);
Writes the appropriate built-in type. Returns TRUE if the write is successful.
RWBoolean Write(const char* i, size_t count); RWBoolean Write(const wchar_t* i, size_t count); RWBoolean Write(const short* i, size_t count); RWBoolean Write(const int* i, size_t count); RWBoolean Write(const long* i, size_t count); RWBoolean Write(const unsigned char* i, size_t count); RWBoolean Write(const unsigned short* i,size_t count); RWBoolean Write(const unsigned int* i, size_t count); RWBoolean Write(const unsigned long* i, size_t count); RWBoolean Write(const float* i, size_t count); RWBoolean Write(const double* i, size_t count);
Writes count instances of the indicated built-in type from a block pointed to by i. Returns TRUE if the write is successful.
RWBoolean Write(const char* string);
Writes a character string, including the terminating null character, from a block pointed to by string. Returns TRUE if the write is successful. Beware of non-terminated strings when using this function.
static RWBoolean Exists(const char* filename, int mode = F_OK);
Returns TRUE if a file with name filename exists and may be accessed according to the mode specified. The mode may be ORed together from one or more of:
F_OK: | "Exists" (Implied by any of the others) |
X_OK: | "Executable or searchable" |
W_OK: | "Writable" |
R_OK: | "Readable" |
If your compiler or operating system does not support the POSIX access() function, then mode X_OK will always return FALSE.
RWFile& operator<<(RWFile&, const RWCString& str);
Saves string str to a virtual stream or RWFile, respectively.
RWFile& operator<<(RWFile&, const RWDate& date);
Saves the date date to a virtual stream or RWFile, respectively.
RWFile& operator<<(RWFile&, const RWInteger& x);
Saves the RWInteger x to a virtual stream or RWFile, respectively.
RWFile& operator<<(RWFile&, const RWTime& t);
Saves RWTime t to a virtual stream or RWFile, respectively.
RWFile& operator>>(RWFile&, RWTime& t);
Restores an RWTime into t from a virtual stream or RWFile, respectively, replacing the previous contents of t.
RWFile& operator>>(RWFile&, RWCString& str);
Restores a string into str from a virtual stream or RWFile, respectively, replacing the previous contents of str.
RWFile& operator>>(RWFile&, RWDate& date);
Restores the date into date from a virtual stream or RWFile, respectively, replacing the previous contents of date.
RWFile& operator>>(RWFile&, RWInteger& x);
Restores an RWInteger into x from a virtual stream or RWFile, respectively, replacing the previous contents of x.
RWFile& operator<<(RWFile& strm, const RWTPtrDeque<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrDlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashMap<K,T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashMultiMap<K,T,H,EQ>& coll); operator<<(RWFile& strm, const RWTPtrHashMultiSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrMap<K,T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrMultiMap<K,T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrMultiSet<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrOrderedVector<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrSet<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrSlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrSortedDlist<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrSortedVector<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValDeque<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValDlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValHashMap<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTValHashMultiMap<K,T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTValHashSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTValMap<K,T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValMultiMap<K,T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValMultiSet<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValOrderedVector<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValSet<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValSlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValSortedDlist<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValSortedVector& coll);
Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.
RWFile& operator>>(RWFile& strm, RWTPtrDeque<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrDlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMap<K,T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiMap<K,T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrMap<K,T,C>& coll); RWFile& operator>>(RWFile& strm, RWTPtrMultiMap<K,T,C>& coll); RWFile& operator>>(RWFile& strm, RWTPtrMultiSet<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTPtrOrderedVector<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrSet<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTPtrSlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrSortedDlist<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTPtrSortedVector<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValDeque<T>& coll); RWFile& operator>>(RWFile& strm, RWTValDlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTValHashMap<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValHashMultiMap<K,T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValHashMultiSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValHashSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValMap<K,T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValMultiMap<K,T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValMultiSet<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValOrderedVector<T>& coll); RWFile& operator>>(RWFile& strm, RWTValSet<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValSlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTValSortedDlist<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValSortedVector<T,C>& coll);
Restores the contents of the collection coll from the input stream strm.
RWFile& operator>>(RWFile& strm, RWTPtrDeque<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrDlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashMap<K,T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiMap<K,T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrMap<K,T,C>*& p); RWFile& operator>>(RWFile& strm, RWTPtrMultiMap<K,T,C>*& p); RWFile& operator>>(RWFile& strm, RWTPtrMultiSet<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTPtrOrderedVector<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrSet<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTPtrSlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrSortedDlist<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTPtrSortedVector<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValDeque<T>*& p); RWFile& operator>>(RWFile& strm, RWTValDlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTValHashMap<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTValHashMultiMap<K,T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTValHashMultiSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTValHashSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTValMap<K,T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValMultiMap<K,T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValMultiSet<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValOrderedVector<T>*& p); RWFile& operator>>(RWFile& strm, RWTValSet<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValSlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTValSortedDlist<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValSortedVector<T,C>*& p);
Looks at the next object on the input stream strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then you are responsible for deleting it.