Solved

Use of CMemFile

Posted on 1999-01-18
5
3,313 Views
Last Modified: 2013-11-20
Hello:
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

TIA
0
Comment
Question by:qocarlos
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Answers2000
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.


0
 
LVL 3

Author Comment

by:qocarlos
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:

abcdefg
efghijk
(...)

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?


0
 
LVL 12

Expert Comment

by:migel
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;
do{
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.
};
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 150 total points
ID: 1327902
gocarlos

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
{
      DECLARE_DYNAMIC(CMemFile)

public:
// 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
protected:
      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
protected:
      UINT m_nGrowBytes;
      DWORD m_nPosition;
      DWORD m_nBufferSize;
      DWORD m_nFileSize;
      BYTE* m_lpBuffer;
      BOOL m_bAutoDelete;

public:
      virtual ~CMemFile();
#ifdef _DEBUG
      virtual void Dump(CDumpContext& dc) const;
      virtual void AssertValid() const;
#endif
      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.
0
 
LVL 3

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now