Click here to Skip to main content
Click here to Skip to main content
Go to top


, 15 May 2002
Rate this:
Please Sign up or sign in to vote.
Disc and memory files within the same object


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, void *m_stream , 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:

long CxFile::GenericMethod()
    if (m_stream==NULL) return -1;
    case 0:
        //operations for FILE* handling
    case 1:
        //operations for BYTE* handling
    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: m_Position, m_Size, m_Edge; rispectively 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 Read and 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), m_stream is 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 Close().

Member Functions

  • void* Open(const char *filename, const char *mode)
  • long Close()
  • size_t Read(void *buffer, size_t size, size_t count)
  • size_t Write(const void *buffer, size_t size, size_t count)
  • long Flush()
  • long Seek(long offset, int origin)
  • long Tell()
  • long Size()
  • long GetPos(fpos_t *pos)
  • long Eof()
  • long Error()
  • long PutC(unsigned char c);
  • long GetC();
  • void* GetStream()
  • void* Alloc(DWORD nBytes)
  • void Free();
  • void Transfer(CxFile &from)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

Comments and Discussions

General"little" error in CxFile::GetC() Pinmembermaya7514-Dec-03 2:21 
GeneralRe: "little" error in CxFile::GetC() PinmemberDavide Pizzolato16-Dec-03 1:06 
GeneralC++ Pinmemberihaml15-Oct-02 3:08 
GeneralRe: C++ PinmemberDavide Pizzolato15-Oct-02 9:13 
QuestionHow about C++ Pinmemberokigan16-May-02 16:14 
AnswerRe: How about C++ PinmemberDavide Pizzolato16-May-02 20:05 
GeneralRe: How about C++ PinmemberAnonymous22-May-02 6:33 
GeneralRe: How about C++ PinmemberDavide Pizzolato2-Jun-02 9:52 
GeneralRe: How about C++ PinmemberAnthony_Yio29-Oct-02 16:52 
QuestionHow about standard C++? PinmemberPaul Selormey16-May-02 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 16 May 2002
Article Copyright 2002 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid