Solved

CMemFile

Posted on 2001-08-01
7
672 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
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.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 85
VB.NET how to use the Vertical ScrollBar 5 90
FizzBuzz challenge 9 77
Not needed 13 96
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 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

19 Experts available now in Live!

Get 1:1 Help Now