Solved

CMemFile

Posted on 2001-08-01
7
679 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
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.

738 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