Click here to Skip to main content
15,888,908 members
Articles / Desktop Programming / MFC
Article

CxFile

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
15 May 20022 min read 101.2K   1K   22   16
Disc and memory files within the same object

Introduction

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;
    switch(m_StorageType){
    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.

Constructors

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)

License

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


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General"little" error in CxFile::GetC() Pin
maya7514-Dec-03 2:21
maya7514-Dec-03 2:21 
you forgot to increment m_Position in this method, as fgetc() does :

<br />
replace :<br />
<br />
long CxFile::GetC()<br />
{<br />
	if (m_stream==NULL) return -1;<br />
	switch(m_StorageType){<br />
	case 0:<br />
		return getc((FILE*)m_stream);<br />
	case 1:<br />
		return *(BYTE*)((BYTE*)m_stream + m_Position); // not good<br />
	}<br />
	return -1;<br />
}<br />
<br />
by :<br />
<br />
long CxFile::GetC()<br />
{<br />
	if (m_stream==NULL) return -1;<br />
	switch(m_StorageType){<br />
	case 0:<br />
		return getc((FILE*)m_stream);<br />
	case 1:<br />
		{<br />
		m_Position++; // added<br />
		return *((BYTE*)m_stream + m_Position - 1); // changed<br />
		}<br />
	}<br />
	return -1;<br />
}<br />


Thanks for you code it is very useful for me ! Smile | :)
GeneralRe: &quot;little&quot; error in CxFile::GetC() Pin
Davide Pizzolato16-Dec-03 1:06
Davide Pizzolato16-Dec-03 1:06 
GeneralC++ Pin
ihaml15-Oct-02 3:08
ihaml15-Oct-02 3:08 
GeneralRe: C++ Pin
Davide Pizzolato15-Oct-02 9:13
Davide Pizzolato15-Oct-02 9:13 
QuestionHow about C++ Pin
Igor Okulist16-May-02 16:14
Igor Okulist16-May-02 16:14 
AnswerRe: How about C++ Pin
Davide Pizzolato16-May-02 20:05
Davide Pizzolato16-May-02 20:05 
GeneralRe: How about C++ Pin
22-May-02 6:33
suss22-May-02 6:33 
GeneralRe: How about C++ Pin
Davide Pizzolato2-Jun-02 9:52
Davide Pizzolato2-Jun-02 9:52 
GeneralRe: How about C++ Pin
Anthony_Yio29-Oct-02 16:52
Anthony_Yio29-Oct-02 16:52 
QuestionHow about standard C++? Pin
Paul Selormey16-May-02 14:25
Paul Selormey16-May-02 14:25 
AnswerRe: How about standard C++? Pin
mystro_AKA_kokie16-May-02 16:44
mystro_AKA_kokie16-May-02 16:44 
AnswerRe: How about standard C++? Pin
Christian Graus16-May-02 17:03
protectorChristian Graus16-May-02 17:03 
GeneralRe: How about standard C++? Pin
Rama Krishna Vavilala16-May-02 17:15
Rama Krishna Vavilala16-May-02 17:15 
GeneralRe: How about standard C++? Pin
Paul Selormey16-May-02 18:29
Paul Selormey16-May-02 18:29 
AnswerRe: How about standard C++? Pin
Davide Pizzolato16-May-02 19:48
Davide Pizzolato16-May-02 19:48 
GeneralRe: How about standard C++? Pin
Brad Bruce17-May-02 2:43
Brad 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.