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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:\MyF ileName.ex t', fmOpenRead);
try
while fs.Position < fs.Size do begin
fs.Read(buf, sizeOf(buf));
Memo1.Lines.Add(inttostr(b uf.ss1)+', 'inttostr(buf.ss2)+', 'inttostr(buf.ss3));
end;
finally
fs.Free;
end;
end;
wbr, mo.
with TFileStream that will be very similar:
type
myrec = record
ss1, ss2, ss3: dword;
end;
procedure TForm1.btnLoadClick(Sender
var
fs: TFileStream;
buf: myrec;
begin
fs := TFileStream.Create('C:\MyF
try
while fs.Position < fs.Size do begin
fs.Read(buf, sizeOf(buf));
Memo1.Lines.Add(inttostr(b
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
Munim
ASKER
Thanks Luka. I got it...
ASKER