Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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?
0
gk123
Asked:
gk123
1 Solution
 
gk123Author Commented:
I had used a filestream to open the file and then read it. but its giving some stream read error
0
 
LukA_YJKCommented:
Hope it will help:
type
  MyRec = record
    ss1, ss2, ss3: LongInt;
  end;
  MyFile = file of MyRec;
...
var F: MyFile;
    R: MyRec;
...
begin
  AssignFile(F, 'C:\Data.dat');
  Reset(F);
  while not EOF(F) do begin
    Read(F, R);
    with R do
    Memo1.Lines.Add(IntToStr(ss1)+IntToStr(ss2)+IntToStr(ss3));
  end;
  CloseFile(F);
end;
0
 
mocartsCommented:
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.
0
 
MunimCommented:
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
0
 
gk123Author Commented:
Thanks Luka. I got it...
0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now