?
Solved

Reading file of byte into memory?

Posted on 1999-08-02
11
Medium Priority
?
236 Views
Last Modified: 2010-04-04
What would be the fastest way to read a file of byte into memory, then parse the file in 60 byte chunks to fill records.....the file CANNOT be read as file of record, as the size of record is sometimes 51 and sometimes 60. Looking to read up to 6000 records this way, and one by one is killing my app speed.
0
Comment
Question by:gelat
11 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1392555
use TMemoryStream and use LoadFromFile()... than you can use something like this.. thne use ReadBuffer() of TMemoryStream to read the amount of data you need...
if  you need an example I'll be more than glad to write one for you.

..-=ViKtOr=-..
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1392556
Ok, here is an example for you :)

ok, i have 4 edit boxes on the form, two of them represent the name, and the other two the age of the person... then I have two butttons... one of them creates the data file, and the other one reads it back and puts it in the edit boxes...

=========
type
    TPerson = record
           Age : byte;
           Name : string[20];
    end;

//this reads it into the edit boxes
procedure TForm1.Button1Click(Sender: TObject);
var
   MemFile : TMemoryStream;
   Me : TPerson;
begin
     MemFile := TMemoryStream.Create;
     MemFile.LoadFromFile('c:\windows\desktop\test.dat');
     MemFile.ReadBuffer(me, sizeof(me));
     Edit1.Text := me.name;
     edit2.text := IntToStr(me.age);
     MemFile.ReadBuffer(me, sizeof(me));
     Edit3.Text := me.name;
     edit4.text := IntToStr(me.age);
     MemFile.Free;
end;

//This creates the data file...

procedure TForm1.Button2Click(Sender: TObject);
var
   Me : TPerson;
   f : tmemorystream;
begin
     Me.Age := 17;
     Me.Name := 'Viktor Ivanov';
     f := tmemorystream.create;
     f.SetSize(sizeof(TPerson)*2);
     f.WriteBuffer(Me, sizeof(me));
     Me.Age := 20;
     Me.Name := 'Whatever';
     f.WriteBuffer(Me, sizeof(me));
     f.SaveToFile('c:\windows\desktop\test.dat');
     f.free;
end;
=============
Good Luck!!

..-=ViKtOr=-..
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1392557
Or if you care about speed more than memory, then allocate a buffer big enough for the entire file.
Read the whole file in one go.

Then cycle through the data in 60/51 byte jumps, using CopyMemory to copy data into your records.

To make it faster still, use pointers to your records, and just set the pointer to the right offset in the buffer.

Cheers,
Phil.

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 10

Accepted Solution

by:
viktornet earned 400 total points
ID: 1392558
btw, I would answer the answer in the hope that my suggestions have helped!!

..-=ViKtOr=-..
0
 
LVL 5

Expert Comment

by:Gwena
ID: 1392559
Thanx Viktor :)
I needed to know how to read a file into memory too....
And the demo really helps...
Glad to see you back on EE ;-)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1392560
Gwena, forgot to logout before you posted the previous comment? ;))

>>I needed to know how to read a file into memory too....
I already showed  you how to do that :)) Here is the line that does that...

MemFile.LoadFromFile('c:\windows\desktop\test.dat');

look it up in the Button1Click() event.. :)) that's exactly what it does.. it loads the file into memory so you can deal with it in there... which is a whole lot faster ;)

..-=ViKtOr=-..
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1392561
haha... my answer makes no sense ;))

It should've read...

"btw, I would answer the QUESTION in the hope that my suggestions have helped!!

      ..-=ViKtOr=-.."

Take Care!

..-=ViKtOr=-..
0
 

Author Comment

by:gelat
ID: 1392562
Thanks - it worked like a charm,.,..never used streams before.....
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1392563
np!

If  you need any help with streams, Gwena, please let me know ;)

..-=ViKtOr=-..
0
 
LVL 5

Expert Comment

by:Gwena
ID: 1392564
Ok Vik I'm letting you know...I need lots of help ;-)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1392565
hehe... anytime, just ask ;)
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

588 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