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.
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
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
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.
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!
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
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>.