CMemFile

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
elitoAsked:
Who is Participating?
 
DanRollinsConnect With a Mentor Commented:
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
 
DanRollinsCommented:
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
 
elitoAuthor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
migelCommented:
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
 
migelCommented:
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
 
elitoAuthor Commented:
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
 
elitoAuthor Commented:
Thanks for your help!!!
0
All Courses

From novice to tech pro — start learning today.