?
Solved

CMemFile

Posted on 2001-08-01
7
Medium Priority
?
686 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
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 1200 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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