Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read numerical data from a binary file ?

359 views
Skip to first unread message

Freddy Aldersons

unread,
Sep 11, 1998, 3:00:00 AM9/11/98
to
Hi,

I want to read numerical data (floating-point and integers)
from a single binary file. I know how many variables there
are and where the floating-points and integers are written.
This is basic in Fortran but does not seem so easy with Delphi.

Could somebody provide me with some lines of code, specially
for reading floating-point values.

TIA,
Freddy Aldersons.

Earl F. Glynn

unread,
Sep 11, 1998, 3:00:00 AM9/11/98
to
Freddy:

Freddy Aldersons wrote in message <35F92D...@netmedia.net.il>...

I agree. This was always trivial to do in Fortran, but has been
hard in most other languages.

You'll need to provide more information.

First, do you know the types of floating point values?
Single, Real, Double?

Are the values IEEE format? There are "old" floating point formats, too.
Microsoft, Inprise, Dec, IBM, ... have proprietary formats from the past.

Do you know the source of the values? On a PC values are
little endian, but on many Unix boxes these values will be
big endian.

I can provide you additional information, once I understand the
question better.

efg
_________________________________________
efg's Computer Lab: http://infomaster.net/external/efg

Earl F. Glynn E-Mail: Earl...@att.net
Overland Park, KS USA

Philippe Ranger

unread,
Sep 11, 1998, 3:00:00 AM9/11/98
to
Freddy: >>I want to read numerical data (floating-point and integers)

from a single binary file. I know how many variables there
are and where the floating-points and integers are written.
This is basic in Fortran but does not seem so easy with Delphi.

Could somebody provide me with some lines of code, specially
for reading floating-point values.<<

If the file is nothing but a series of identical blocks, you can do it this
way:

Type
Trecord=record
array[1..10] of bytes;
i: integer;
d: double;
end;

Var
f: file of Trecord;
r: Trecord;
Begin
assignFile(f, 'myFile');
reset(f);
while not eof(f) do begin
read(f, r);
//do your thing with r
end;
close(f);
End;

The record type is just a model, with an array to skip bytes if you need to
(don't use it otherwise, and do use as many as there are sub-blocks to
skip). You have to know the actual int and float formats used, and define
your fields accordingly. And of course you can have as many as you want.

If you just want to read a double from a position j, you could do this:

Var
f: file;
d: double;
Begin
assignFile(f, 'myFile');
reset(f, 1);
seek(f, j); //this is from 0
blockRead(f, d, sizeof(d));
....etc.

PhR

Glynn Owen

unread,
Sep 11, 1998, 3:00:00 AM9/11/98
to
What kind of floating-point?

What size of integer?

Glynn

Freddy Aldersons wrote:
>
> Hi,


>
> I want to read numerical data (floating-point and integers)
> from a single binary file. I know how many variables there
> are and where the floating-points and integers are written.
> This is basic in Fortran but does not seem so easy with Delphi.
>
> Could somebody provide me with some lines of code, specially
> for reading floating-point values.
>

> TIA,
> Freddy Aldersons.

--
Linus's Law: Given enough eyeballs, all bugs are shallow.

Peter Below

unread,
Sep 11, 1998, 3:00:00 AM9/11/98
to
> I want to read numerical data (floating-point and integers)
> from a single binary file. I know how many variables there
> are and where the floating-points and integers are written.
> This is basic in Fortran but does not seem so easy with Delphi.
>
> Could somebody provide me with some lines of code, specially
> for reading floating-point values.
>
Freddy,

select the correct float type, e.g. Double. Declare a variable of this
type.

Using a TFilestream (named fs) the reading would be done with

fs.read( aDoubleVar, Sizeof( aDoubleVar ));

If an integer is next again select the correct integer type (SmallInt for 2
byte integers, Longint or Integer for 4 byte integers):

fs.read( anIntegerVar, sizeof( anIntegervar ));

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitely requested!


Philippe Ranger

unread,
Sep 13, 1998, 3:00:00 AM9/13/98
to
Peter: >>Using a TFilestream (named fs) the reading would be done with

fs.read( aDoubleVar, Sizeof( aDoubleVar ));<<

Peter, my feeling is that TfileStream (and Tstream) come into their own when
dealing with mixed values, or with complex values that need their own read
and write transforms, or of course for code that reads indifferently from
file or memory. Ordinary file vars do pre-OO stuff generally in a simpler
way — I feel.

Am I wrong, or are you using TfileStream simply because it is the "standard"
file-access type, despite the added complication in this particular case?

PhR


Peter Below

unread,
Sep 14, 1998, 3:00:00 AM9/14/98
to

Phillip,

the question as i understood it does relate to a file that has mixed binary
content. If the content can be mapped to a Pascal record type a File of
recordtype would certainly be easier to use. But if it would boil down to an
untyped file and Blockread i would prefer a TFilestream. The OO syntax appeal
to me <g>.

0 new messages