Link to home
Start Free TrialLog in
Avatar of gk123
gk123

asked on

How to read a binary data file

I have a binary data file in my disk, where each record is of 12 bytes. The data in the binary file can be read with a structure as follows
struct {
         unsigned long int ss1;   (4 bytes)
         unsigned long int ss2;   ( 4 bytes)
         unsigned long int ss3;   (4 bytes)
       }myrec

How can I read each record ie, 12 bytes of data from this file in Delphi and display each record in a memo field?
Avatar of gk123
gk123

ASKER

I had used a filestream to open the file and then read it. but its giving some stream read error
ASKER CERTIFIED SOLUTION
Avatar of LukA_YJK
LukA_YJK
Flag of Azerbaijan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just to let gk123 compare with he code.
with TFileStream that will be very similar:

type
  myrec = record
    ss1, ss2, ss3: dword;
  end;

procedure TForm1.btnLoadClick(Sender: TObject);
var
  fs: TFileStream;
  buf: myrec;
begin
  fs := TFileStream.Create('C:\MyFileName.ext', fmOpenRead);
  try
    while fs.Position < fs.Size do begin
      fs.Read(buf, sizeOf(buf));
      Memo1.Lines.Add(inttostr(buf.ss1)+', 'inttostr(buf.ss2)+', 'inttostr(buf.ss3));
    end;
  finally
    fs.Free;
  end;
end;

wbr, mo.
Open a file, declare the file flag to "var f: file". use "reset(f, 1)". Declare "var p: pointer". Now use "BlockRead(p, 12)". copy the pointer to the structure. Copymemory(@yourstruc, p, 12). close the file "CloseFile(f)". Thats all. THIS METHOD IS THE FASTEST IN WIN32 SYSTEM.

Munim
Avatar of gk123

ASKER

Thanks Luka. I got it...