(Solved) creating a binary fiile lazarus / 61-Beginners ---------------------------------------------------------------------- speter · 2020-10-13 06:36 G'Day Folks, I am trying to create a program to create a binary file. The structure of the file is:   1 x 32bit integer; then   100 x 32bit single. My original thought was to go for a "file of byte"; but I'm not sure how to write the integer or the reals as bytes (that is I'm not sure whether it would be hi-bit first (or last)). The first integer gives the size of the remaining data; in this case it will be "10" (the data is 10x10)... Any suggestions!? Can I (simply) do something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i assignfile(f,'foo.dat');rewrite(f);write(f,size);for a := 0 to 99 do write(f,d[a]);closefile(f); The other thing I am unsure about is making sure I write 32bit info... cheers S. ---------------------------------------------------------------------- speter · 2020-10-13 07:16 Further to the above included below is a first attempt using an example @ https://wiki.freepascal.org/typed_files (File handling "typed files") section = Write Data Record. My code is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);const  sz = 9;type  dtype = record            size : integer;            data : array [0..sz,0..sz] of single;          end;var  d : dtype;   procedure around(x,y : byte);  var    a,b, kx,ky : integer;  begin    for a := -1 to 1 do      begin        ky := y + a;        if (ky in [0..sz]) then          for b := -1 to 1 do            begin              kx := x + b;              if (kx in [0..sz]) then                if d.data[kx,ky] = 0 then                  d.data[kx,ky] := min(1.0,max(0.0,d.data[x,y] + (random / 5) - 0.1));            end;    end;  end; var  a, x,y : byte;  f : file of dtype;  s : string;begin  randomize;  d.size := sz+1;   for y := 0 to sz do    for x := 0 to sz do      d.data[x,y] := 0;   d.data[random(10),random(10)] := random;   for a := 0 to 19 do    for y := 0 to sz do      for x := 0 to sz do        if d.data[x,y] > 0 then around(x,y);   for y := 0 to sz do    begin      s := '';      for x := 0 to sz do        s := s + format(' %5.3f',[d.data[x,y]]);      memo1.lines.add(y.tostring+':'+s);    end;   assignfile(f,'foo.dat');  rewrite(f);  write(f,d);  closefile(f);  memo1.lines.add('"foo.dat" created.');end; The file is 404 bytes, 101 x 4 bytes, so I guess it is correct! ---------------------------------------------------------------------- Handoko · 2020-10-13 07:23 Alternatively you can convert Integer or Single to 4-bytes array by using combination of Pointer and absolute: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);type  T4Bytes = array[0..3] of Byte;var  PLongInt: ^LongInt; // Use LongInt to make sure it is a 4-bytes Integer  P4Bytes:  ^T4Bytes absolute PLongInt; // Use absolute to point it to PLongInt  Test:     LongInt;  i:        Integer;begin  Test := 1; // Put some value here for testing  PLongInt := @Test;  for i := 0 to 3 do    ShowMessage(IntToStr(P4Bytes^[i])); // P4Byte^[0..3] has the data of Testend; ---------------------------------------------------------------------- speter · 2020-10-13 11:55 Thanks very much for answering Handoko. The file-of-record approach worked for me and is nice and simple. cheers S. ---------------------------------------------------------------------- ASerge · 2020-10-13 15:34 Quote from: speter on October 13, 2020, 01:55:35 pmThe file-of-record approach worked for me and is nice and simple. For file IO operations, it is better always declare record with the packed prefix, and explicitly specify the size types. This will help you avoid problems in the future. In this case, you are lucky, because the size of the Single type is no larger than the size of the Integer type. For example, if you take the Double type instead of the Single type, the size will not be 4 + 8*100, but 8 + 8*100 (on Windows platform). For greater clarity, it is better to write packed even for arrays, although this is already implied: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  DataType = packed record    Size: Int32;    Data: packed array[0..99] of Single;  end; ---------------------------------------------------------------------- speter · 2020-10-13 22:24 Thanks for the reply, ASerge. Is it possible to explicitly declare a 32-bit real type? I know that 'single' _is_ a 32-bit type, but (for example) 'int32' is explicitly so. The docs (https://www.freepascal.org/docs-html/ref/refsu5.html) don't include a real type "named" as 32-bit. cheers S. ---------------------------------------------------------------------- jamie · 2020-10-14 11:25 a single is a 32 bit real number as pointed out.. if you want to makeup some fancy new name to it... you can do this.. Type    My32BitReal = single; and from there on the compiler will just use single in place of your type.. and for what @ASerge pointed out was just a define in the libs basically done the same way  Int32 = longint; Did I miss something ? ---------------------------------------------------------------------- speter · 2020-10-15 05:44 Thanks for the reply Jamie. I was really thinking about what might happen in the future... I don't think "int32" will ever stop being 32-bit; but conceivably "single" could one day become 64-bit floating number... but I guess lots of programs would stop working... so it probably will not happen. cheers S.