Solved

In memory file

Posted on 2004-08-19
7
322 Views
Last Modified: 2010-04-05
Is there a way to create a "virtual" file that is in memory only. What I would like to do is somehow create a section of memory that can be read and written to like a standard file using the file functions Sysutils.FileRead,Sysutils.FileWrite,Sysutils.Fileseek, etc...

The reason I wan to do this is I already have a class written that uses a temporary file as a buffer, but I would like to utilize RAM first if it is available.
0
Comment
Question by:rbohac
7 Comments
 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 40 total points
ID: 11843166
not exactly those functions, but you can use MMF (Memory mapped files)

there are many documents about that...

http://www.delphipages.com/news/detaildocs.cfm?ID=100
0
 
LVL 3

Assisted Solution

by:BJThomsen
BJThomsen earned 40 total points
ID: 11846152
If you rewrote your class to use a TStream, you could choose TFileStream or TMemoryStream interchangably.
0
 
LVL 6

Accepted Solution

by:
gwalkeriq earned 80 total points
ID: 11846788

Memory mapped files have some limitation that make them awkward to work with, specifically, dynamically changing the size of the file once is is created.

If you have a lot of complex code using FileRead, FileWrite, etc. it is not too bad to create your own class based on tMemoryStream, and then create FileRead, FileWrite, etc. methods for the stream class.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Assisted Solution

by:Molando
Molando earned 40 total points
ID: 11846855
if you are going to share your data between programs then you need to go down the memory mapped file route (or pass the data with wm_copydata. wm_copy data if it is just a small bit of data. Memory mapped files are likefiles on your harddrive, but just in memory. Only big problem with them is you can not resize them .
if you do not need to share the data between programs, then the TStream class as stated by BJThomsen is the best way to go.
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 50 total points
ID: 11848160
????
You can use RAM if you want,  as a pointer (from a  GetMem( ) ), , , and Then the TFileStream has a Write and WriteBuffer method that you can get the pointer Data to the file, you will need to dereference the pointer^, but you will need to know the size of the memoery block you want to write. .
( I think you could use the  FileWrite  and just dereference the pointer^)
 or I would use a TMemoryStream, ,
I beleive the TMemoryStream has a SaveToFile method, which writes the entire memory block to a file
0
 
LVL 6

Assisted Solution

by:gwalkeriq
gwalkeriq earned 80 total points
ID: 11852249
I should also remind you that if you use a memorystream, and it is too big to fit into RAM, the operating system will write it to disk as backup store -- This is virtual memory, it generally works well. Thus, you should write to memory for temporary buffers, write to disk for persistance.
0
 
LVL 6

Author Comment

by:rbohac
ID: 12006416
All of these answers were very insightful. Thank you!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
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

9 Experts available now in Live!

Get 1:1 Help Now