Solved

In memory file

Posted on 2004-08-19
7
329 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 34

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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