Board index » delphi » please help to dump a real number to a file as binary data

please help to dump a real number to a file as binary data

Hi all,

I am a C programmer and I need to convert/read some data that seems to have
been created with Pascal and some where in the data I think are real
numbers in binary format. In order for me to be able to test my conversion
routine I would like to be able to create a Delphi program to dump real
numbers to a file. I would be so grateful if some one could show me how to
do that, thanks. The Delphi/Pascal program/code that I need in C terms
would basically be like:

float var1 = 23.56;
FILE *fp;

fp = fopen ("file.bin", "w");
fwrite (&var1, sizeof (float), 1, fp);              // writes a binary dump of var1 to
file
fclose(fp);

how to do that in Delphi?

thanks in advance
jono

 

Re:please help to dump a real number to a file as binary data


    Jono wrote in message <01bddb18$ba87c540$d70165cb@jng>...
    >>I am a C programmer and I need to convert/read some data that seems to
have been created with Pascal and some where in the data I think are real
numbers in binary format. In order for me to be able to test my conversion
routine I would like to be able to create a Delphi program to dump real
numbers to a file. I would be so grateful if some one could show me how to
do that, thanks. The Delphi/Pascal program/code that I need in C terms would
basically be like:

    float var1 = 23.56;
    FILE *fp;

    fp = fopen ("file.bin", "w");
    fwrite (&var1, sizeof (float), 1, fp); // writes a binary dump of var1
to file
    fclose(fp);

    how to do that in Delphi? <<

    Like this:
      type
        { declare a file type for whatever floating-point type you need -
neater code}
         TFloatFile : file of single;

    procedure WriteFloat(Value : single);
    var
      f : TFloatFile;
    begin
      AssignFile(f, 'file.bin'); //assign a name to the file variable
      Reset(f); //open the file (see also ReWrite)

      Seek(f, FileSize(f)); //go to the end of the file
      Write(f, Value); //write the value to the file

      CloseFile(f); //of course...
    end;

    HTH

    --
    Jeremy Collins
    Kansai Business Systems

Re:please help to dump a real number to a file as binary data


Thanks Jeremy, much appreciate your help

regards
jono

Re:please help to dump a real number to a file as binary data


Quote
Jono wrote:

> Hi all,

> I am a C programmer and I need to convert/read some data that seems to
> have been created with Pascal and some where in the data I think are
> real numbers in binary format. In order for me to be able to test my
> conversion routine I would like to be able to create a Delphi program
> to dump real numbers to a file. I would be so grateful if some one
> could show me how to do that, thanks. The Delphi/Pascal program/code
> that I need in C terms would basically be like:

Okie. Well, just a few quick questions.

1. At the last count there are four floating point formats that may be
used in Delphi. Any idea which? If you can give us an idea of the size
in bytes, then we can hazard a quess.

2. Three of those fornats are standard not only amongst all intel
machines, but are also compliant with the IEEE floating point standards.
If you know it's a float, and you know what size it is, your C program
should be able to read it directly. If you program in C on a big endian
machine, such as a Sun workstation, then reverse the byte order, and
just read it straight into your beloved FPU.

Anyway... perhaps something like this.

BTW. I don't guarantee this doesn't have any syntax errors!
It might just work under delphi 2.
Please excuse my using the file stream object, but I just can't remember
the old pascal file handling functions.

program floattest;

{$APPTYPE CONSOLE}

uses Classes;

var
        float0:real;     {pascal specific, 6 bytes}
        float1:single;   {IEEE standard 4 bytes}
        float2:double;   {IEEE standard 8 bytes}
        float3:extended; {IEEE standard 10 bytes}
        Out:TFileStream;

begin
float0:=23.56;
float1:=23.56;
float2:=23.56;
float3:=23.56;
Out:=TFileStream.Create('c:\test.bin',fmCreate);
try
        with Out do
        begin
                WriteBuffer(@float0,SizeOf(float0));
                WriteBuffer(@float1,SizeOf(float1));
                WriteBuffer(@float2,SizeOf(float2));
                WriteBuffer(@float3,SizeOf(float3));
        end;
finally
        Out.Free;
end;
end;

HTH. Martin H.

Other Threads