Solved

CMemFile

Posted on 2001-08-01
7
670 Views
Last Modified: 2013-11-20
Hi,

I have a function that creates file using CFile:

bool SaveFile(LPCTSTR FileName)
{
     if (!file.Open(FileName,CFile::modeWrite |CFile::modeCreate|CFile::shareDenyWrite))
          return false;

(...)

}

Now I would like to create the file in memory using CMemFile.

How can I do that?

Thanks
0
Comment
Question by:elito
  • 3
  • 2
  • 2
7 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6341134
Basically, you just create the CMemFile object and then use the objects Read and Write fns:


CMemFile gcMemFile; // Ready to use - no Open necessary.

... in some fn ...

CString s="Hi there\r\n";
gcMemFile.Write( (LPCSTR)s, s.GetLength() );

...
-=-==-=-=-=-=-=-=-=-=-
In some other fn...
-=-==-=-=-=-=-=-=-=-=-

gcMemFile.SeekToBegin();

char buf[100];
gcMemFile.Read( buf, 100 );
CString s2=buf;

ASSERT( s2==s );

-=-=-=-=-==-
If you need more info, of if this comment is an inadequate answer, please add a post describing what you specifically need to know.  Thanks.

-- Dan
0
 

Author Comment

by:elito
ID: 6341295
Hi Dan,

Thanks a lot for your comment, that's exactly what I wanted to know!!

However there is still something I'd like to know.
What would happen if the file is so big that doesn't fit in memory? For instance, the file size is 80 MB and the available memory is 64 MB. Will this make the program to fail?
Also, is there a way to optimize the number of bytes to grow the file (1024 by default) ?

Thanks,
elito
0
 
LVL 12

Expert Comment

by:migel
ID: 6341499
Hi!
answer for second queestion:
In this case Memory file become very inefficient since Windows start swap file using (virtual memory on the disk), so all advantages of the Memory file will be losses.
In this case (for very large files) I recommend you use pagging mechanism. That is you create memory block that will represent sliding window in the disk file and fill it(or flush it) from/to the disk when program need perform such operation.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 12

Expert Comment

by:migel
ID: 6341515
also to set size for file growing you can use parameter of the CMemFile constructor that is:
CMemFile gcMemFile(100); //set grow size to 100 bytes
0
 

Author Comment

by:elito
ID: 6341549
Thanks migel,

Actually what I really care is that the program doesn't crash. What I'm trying to do is to transfer a file from my application to a third party application. I'm using memory mapped files. Up till now what I was doing was creating a temporary file and then transfering the file by using CreateFileMapping and MapViewOfFile.
This works fine now, I just wanted to know what would happen if the file were too large.

Thanks
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
ID: 6347224
When the CMemFile file is large, the system will swap chunks of it out to disk.  That's not so bad -- the overhead will be about the same as if you were using a 50 MB disk file.  

However, you will see *overall* system degradation when the OS decides to swap out pieces of the Operating System and running services, DLLs, etc.

-- Dan
0
 

Author Comment

by:elito
ID: 6349503
Thanks for your help!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

8 Experts available now in Live!

Get 1:1 Help Now