Use of CMemFile

Posted on 1999-01-18
Last Modified: 2013-11-20
I want to use the class CMemFile in order to read a entire file into memory and then then work with it in place in memory but I don't know how to work with it.
I don't see how to open a file (for instance "c:\myfile.txt") using the CMemFile constructor.
Any help, source code would be very apreciated

Question by:qocarlos
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 1327899
I think you misunderstand CMemFile

Basically the idea of this class is it represents a block of RAM, and then you can use the file operations to access the RAM.

This is handy for the reason you state, or because you can write code that works with memory or disk based files.

Okay now to your specific problem - there isn't a single call (that I know of) to open a disk file and put into a CMemFile object.  What you need to do is :-

1. Open the disk file
2. Read it into memory
3. Close the disk file
4. Construct a CMemFile
5. Use the Attach member of CMemFile to link the memory to the CMemFile object.


Author Comment

ID: 1327900
I am not totally sure if I did get your right.
This is what I guess you mean:

//Open the file
CString strFileName = "myfile.txt";
CFile myFile;
CFileException fileException;

if ( !myFile.Open( strFileName , CFile::modeRead ), &fileException )
    TRACE( "error");
//read the file into memory
BYTE*        pBuffer;
//allocate pBuffer to the maximum size of
//the file
myFile.Read( pBuffer, sizeof( pBuffer) );

//close the file
//Actually the file will automatically be closed and then //destroyed when it goes out of scope

//Construct a CMemFile an attach attach the block of memory
CMemFile memfile( pBuffer, sizeof(pBuffer));


If the above code is ok, how could I read now a string?
What I mean is that if I have a file like the following:


how can I process each line?

What I was doing til now was to read each line using fgets, but this approach was too slow since it had to access too many times to the disk (the file could have more than 5000 lines). This is why I thought in reading first everything to the memory.

Any idea?

LVL 12

Expert Comment

ID: 1327901
You can use standard file methods to access data in the CMemFile.
That is Read(), Write(),Seek() and others.
<<to process line
for example:
chDelimeter = '\n'; //<- or any you wish
char szBufer[SZ_MAX_LINE_LENGTH] = "";
BYTE symbol = ;
DWORD dwCount = 0;
int nPos;
while (dwCount < memfile.GetLength())
nPos = 0;
dwCount+= memfile.Read(&symbol, 1);
szBufer[nPos++] = symbol;
}while (symbol != chDelimeter);
szBuffer[nPos-=2] = '\0'; //<- remove delimeter
// here you got in the szBuffer entire line.

Accepted Solution

Answers2000 earned 150 total points
ID: 1327902

In your code, This line
CMemFile memfile( pBuffer, sizeof(pBuffer));

attaches the CMemFile to the memory you just read in (this version of the CMemFile construction includes an implicit Attach - step #5 of my first answer - see the VC help if you don't believe me)

Then you just use the Read member of CMemFile to get the data out of the file byte-by-byte.

CMemFile includes over-ridden versions of the Seek and Read members of CFile (i.e. they do the same job).

Incidentally you can read the source for CMemFile in FILEMEM.CPP in the MFC Source directory (if you want to know how it works), or the code around line 1300 (VC5, might be diff on VC6) of AFX.H

This is the class definition of CMemFile

class CMemFile : public CFile

// Constructors
      CMemFile(UINT nGrowBytes = 1024);
      CMemFile(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);

// Operations
      void Attach(BYTE* lpBuffer, UINT nBufferSize, UINT nGrowBytes = 0);
      BYTE* Detach();

// Advanced Overridables
      virtual BYTE* Alloc(DWORD nBytes);
      virtual BYTE* Realloc(BYTE* lpMem, DWORD nBytes);
      virtual BYTE* Memcpy(BYTE* lpMemTarget, const BYTE* lpMemSource, UINT nBytes);
      virtual void Free(BYTE* lpMem);
      virtual void GrowFile(DWORD dwNewLen);

// Implementation
      UINT m_nGrowBytes;
      DWORD m_nPosition;
      DWORD m_nBufferSize;
      DWORD m_nFileSize;
      BYTE* m_lpBuffer;
      BOOL m_bAutoDelete;

      virtual ~CMemFile();
#ifdef _DEBUG
      virtual void Dump(CDumpContext& dc) const;
      virtual void AssertValid() const;
      virtual DWORD GetPosition() const;
      BOOL GetStatus(CFileStatus& rStatus) const;
      virtual LONG Seek(LONG lOff, UINT nFrom);
      virtual void SetLength(DWORD dwNewLen);
      virtual UINT Read(void* lpBuf, UINT nCount);
      virtual void Write(const void* lpBuf, UINT nCount);
      virtual void Abort();
      virtual void Flush();
      virtual void Close();
      virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0,
            void** ppBufStart = NULL, void** ppBufMax = NULL);

      // Unsupported APIs
      virtual CFile* Duplicate() const;
      virtual void LockRange(DWORD dwPos, DWORD dwCount);
      virtual void UnlockRange(DWORD dwPos, DWORD dwCount);

You can see it overrides most functions in CFile.  You should be aware some of the functions (that don't magic logical sense on a block of memory) simply throw an exception and do nothing else, but the basics (Read, Write, Seek) are all there.

Having said all this the other alternative to construction the CMemFile is to just use pBuffer as a normal block of memory, and step thru it using normal pointer type stuff.

Author Comment

ID: 1327903
Thanks a lot.
Now I know how to continue importing and processing my files

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question