Recent

Author Topic: array of byte to file  (Read 2775 times)

mihey78

  • Newbie
  • Posts: 4
array of byte to file
« on: July 24, 2017, 01:51:32 pm »
I have problems saving the array to a file:
Var
   Form1: TForm1;
   S_arr: array [0..15] of byte;
   D_arr: array of byte;
Procedure TForm1.Button1Click (Sender: TObject);
Var i: integer;
   F: file of byte;
   Fn: string;
   Size: int64;
   D_lock_arr: array of byte;
Begin
   Setlength (d_arr, 16);
   Setlength (d_lock_arr, 16);
   For i: = 0 to 15 do begin
     S_arr : = $ A5;
     D_arr : = $ A5;
   End;
   //
    Size: = length (s_arr);
    Fn: = GetCurrentDir + '\ s.bin';
    Assignfile (f, fn);
    Rewrite (f, 1);
    Blockwrite (f, s_arr, size);
    Closefile (f);
    //
    Size: = length (d_arr);
    Fn: = GetCurrentDir + '\ d.bin';
    Assignfile (f, fn);
    Rewrite (f, 1);
    Blockwrite (f, d_arr, size);
    Closefile (f);
    //
    Size: = length (d_lock_arr);
    Fn: = GetCurrentDir + '\ d_loc.bin';
    Assignfile (f, fn);
    Rewrite (f, 1);
    Blockwrite (f, d_lock_arr, size);
    Closefile (f);
End;
Result: three different files

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: array of byte to file
« Reply #1 on: July 24, 2017, 02:18:04 pm »
humm?
Speak postscript or die!
Translate to pdf and live!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: array of byte to file
« Reply #2 on: July 24, 2017, 02:21:36 pm »
I have problems saving the array to a file:
Code: Pascal  [Select][+][-]
  1. Var
  2.    Form1: TForm1;
  3.    S_arr: array [0..15] of byte;
  4.    D_arr: array of byte;
  5. Procedure TForm1.Button1Click (Sender: TObject);
  6. Var i: integer;
  7.    F: file of byte;
  8.    Fn: string;
  9.    Size: int64;
  10.    D_lock_arr: array of byte;
  11. Begin
  12.    Setlength (d_arr, 16);
  13.    Setlength (d_lock_arr, 16);
  14.    For i: = 0 to 15 do begin
  15.      S_arr : = $ A5;
  16.      D_arr : = $ A5;
  17.    End;
  18.    //
  19.     Size: = length (s_arr);
  20.     Fn: = GetCurrentDir + '\ s.bin';
  21.     Assignfile (f, fn);
  22.     Rewrite (f, 1);
  23.     Blockwrite (f, s_arr, size);
  24.     Closefile (f);
  25.     //
  26.     Size: = length (d_arr);
  27.     Fn: = GetCurrentDir + '\ d.bin';
  28.     Assignfile (f, fn);
  29.     Rewrite (f, 1);
  30.     Blockwrite (f, d_arr, size);
  31.     Closefile (f);
  32.     //
  33.     Size: = length (d_lock_arr);
  34.     Fn: = GetCurrentDir + '\ d_loc.bin';
  35.     Assignfile (f, fn);
  36.     Rewrite (f, 1);
  37.     Blockwrite (f, d_lock_arr, size);
  38.     Closefile (f);
  39. End;
  40.  
Result: three different files
Provide a compilable code. in the mean time do not use the array variable use the first element of the array on the blockwrite and see if that helps eg
Code: Pascal  [Select][+][-]
  1.  
  2.     Blockwrite (f, s_arr[0], size);
  3.      ...
  4.     Blockwrite (f, d_arr[0], size);
  5.     ...
  6.     Blockwrite (f, d_lock_arr[0], size);
  7.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: array of byte to file
« Reply #3 on: July 24, 2017, 03:43:41 pm »
I have problems saving the array to a file:
   S_arr: array [0..15] of byte;
   D_arr: array of byte;
Let's start with the basics:
 S_arr *must* be a packed array[0..15] of byte
 D_arr? Do you really mean a dynamic array?
 Why do you not use an intermediate type? Type T_arr = packed array[0..15] of byte;F:file of T_arr?

Etc, etc.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018