Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading file of byte into memory?

Posted on 1999-08-02
11
Medium Priority
?
233 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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