Click here to Skip to main content
13,086,662 members (55,471 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 15 May 2002


, 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

You may also be interested in...

Comments and Discussions

General"little" error in CxFile::GetC() Pin
maya7514-Dec-03 2:21
membermaya7514-Dec-03 2:21 
GeneralRe: "little" error in CxFile::GetC() Pin
Davide Pizzolato16-Dec-03 1:06
memberDavide Pizzolato16-Dec-03 1:06 
GeneralC++ Pin
ihaml15-Oct-02 3:08
memberihaml15-Oct-02 3:08 
GeneralRe: C++ Pin
Davide Pizzolato15-Oct-02 9:13
memberDavide Pizzolato15-Oct-02 9:13 
QuestionHow about C++ Pin
okigan16-May-02 16:14
memberokigan16-May-02 16:14 
AnswerRe: How about C++ Pin
Davide Pizzolato16-May-02 20:05
memberDavide Pizzolato16-May-02 20:05 
GeneralRe: How about C++ Pin
Anonymous22-May-02 6:33
memberAnonymous22-May-02 6:33 
GeneralRe: How about C++ Pin
Davide Pizzolato2-Jun-02 9:52
memberDavide Pizzolato2-Jun-02 9:52 
GeneralRe: How about C++ Pin
Anthony_Yio29-Oct-02 16:52
memberAnthony_Yio29-Oct-02 16:52 
QuestionHow about standard C++? Pin
Paul Selormey16-May-02 14:25
memberPaul Selormey16-May-02 14:25 
AnswerRe: How about standard C++? Pin
Edd16-May-02 16:44
memberEdd16-May-02 16:44 
AnswerRe: How about standard C++? Pin
Christian Graus16-May-02 17:03
memberChristian Graus16-May-02 17:03 
GeneralRe: How about standard C++? Pin
Rama Krishna16-May-02 17:15
memberRama Krishna16-May-02 17:15 
GeneralRe: How about standard C++? Pin
Paul Selormey16-May-02 18:29
memberPaul Selormey16-May-02 18:29 
AnswerRe: How about standard C++? Pin
Davide Pizzolato16-May-02 19:48
memberDavide Pizzolato16-May-02 19:48 
GeneralRe: How about standard C++? Pin
Brad Bruce17-May-02 2:43
memberBrad Bruce17-May-02 2:43 

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

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

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