Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Use of CMemFile

Posted on 1999-01-18
Medium Priority
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 600 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…

664 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