Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

577 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