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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host to IP 7 80
Separate files macro - Errors on larger data set 18 73
modThree challenge 4 96
How to convert MFC::CString to UTF8 wchar_t* 10 278
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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