?
Solved

How to read a binary data file

Posted on 2003-03-24
5
Medium Priority
?
290 Views
Last Modified: 2010-04-04
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
Comment
Question by:gk123
[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
5 Comments
 

Author Comment

by:gk123
ID: 8200446
I had used a filestream to open the file and then read it. but its giving some stream read error
0
 
LVL 3

Accepted Solution

by:
LukA_YJK earned 225 total points
ID: 8200535
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
 
LVL 9

Expert Comment

by:mocarts
ID: 8204051
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
 
LVL 1

Expert Comment

by:Munim
ID: 8205125
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
 

Author Comment

by:gk123
ID: 8232481
Thanks Luka. I got it...
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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