CxFile is a C++ class to manage disc and memory files within the
same object. It uses the standard C run-time functions (no Windows APIs or MFC
classes), so it is very simple to understand how it works. It's also very simple
to add new storage types.
Principle of function
CxFile has a protected member variable,
, it can be a
FILE* or a
BYTE*. The gender of the pointer
is stored in a
m_StorageType variable, assigned only once in the
lifetime of the object.
The structure of a generic method is:
if (m_stream==NULL) return -1;
For disc file handling,
CxFile acts as a wrapper for the stream
I/O routines. For memory file handling, some additional variables are required:
the position of the file pointer, the size of the file, the size of the memory
buffer. Each member must take care of these variables to ensure the reliability
of the data, the
Write methods are the most
critical for this aspect.
The class offers 3 constructors.
CxFile(long type) initializes the object for disc files (type=0)
or memory files (type=1),
NULL, so you must call
Open to create the file.
CxFile(FILE* pFile) attaches the object to an open file.
CxFile(BYTE* pBuffer, DWORD size) attaches the object to an
existing memory buffer.
The main difference between the first and the other two constructors, is that
the first one will close/free
m_stream in the destructor, while the
others doesn't release
m_stream, even if you call explicitly
void* Open(const char *filename, const char *mode)
size_t Read(void *buffer, size_t size, size_t count)
size_t Write(const void *buffer, size_t size, size_t count)
long Seek(long offset, int origin)
long GetPos(fpos_t *pos)
long PutC(unsigned char c);
void* Alloc(DWORD nBytes)
void Transfer(CxFile &from)