Results 1 to 4 of 4

Thread: save/load a dynamic array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    save/load a dynamic array

    Hi, the save-procedure saves obviously 4 records, but the load-proc loads just one from the file test.DAT.
    First thought might be that the load-proc doesn't work correctly, but the file test.MAP is read properly.
    Second thought might be the save-proc isn't well programmed. I can't figure it out. I apriciate any valid hint.
    Note: --- Manage Attachments --- isn't working so here is the code, if you need all files feel free to write to guido-lang@gmx.de
    Thanks in advance

    Code:
    unit FMain;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Buttons;
    
    const
      REC_COUNT = 4;
    
    type
      TRMap = record
         X, Y, Index: Integer;
      end;
    
      TForm1 = class(TForm)
        LoadDat: TBitBtn;
        Save: TButton;
        ListBox1: TListBox;
        LoadMap: TButton;
        procedure LoadDatClick(Sender: TObject);
        procedure SaveClick(Sender: TObject);
        procedure LoadMapClick(Sender: TObject);
      private
        { Private declarations }
       FS: TFileStream;
       ARMap: array of TRMap;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    
    implementation
    
    {$R *.DFM}
    
    procedure TForm1.LoadDatClick(Sender: TObject);
    var
    i, FileSize: Integer;
    begin
    try
        FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmOpenRead);
      except
        ShowMessage('Couldn''t open file.');
        Exit;
      end;
         FS.ReadBuffer(FileSize, SizeOf(FileSize));
    //     Fs.ReadBuffer(FileSize, FS.Size div SizeOf(Integer));
         SetLength(ARMap, FileSize);
        FS.ReadBuffer(ARMap[0], SizeOf(TRMap) * FileSize);
    
      Listbox1.Clear;
      for i := 0 to FileSize-1 do
      begin
      {
      FS.ReadBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
      FS.ReadBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
      FS.ReadBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
      }
      with Listbox1.Items do
      begin
      Add(' ');
      Add(' *** Record NO: ' + InttoStr(i));
      Add(' ');
      Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
      Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
      Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
      end; // with
      end; // for
     FS.Free;
    end; // procedure
    
    
    procedure TForm1.SaveClick(Sender: TObject);
    var
    i, liX, liY, liL: Integer;
    begin
      SetLength(ARMap, REC_COUNT);
      FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmCreate);
      // save record number
      // FS.WriteBuffer(ARMap, SizeOf(Length(ARMap)));
    
      Listbox1.Clear;
    
      //  for i := 0 to REC_COUNT-1 do
      for i := 0 to Length(ARMap)-1 do
      begin
      ARMap[i].X := (i+1);
      ARMap[i].Y := (i+1);
      ARMap[i].Index := (i+1);
    
      with Listbox1.Items do
      begin
      Add(' ');
      Add(' *** Record NO: ' + InttoStr(i));
      Add(' ');
      Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
      Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
      Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
      end; // with
    
    // just one rec: FS.WriteBuffer(ARMap[i], SizeOf(ARMap[i]));
    {  just one rec
      FS.WriteBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
      FS.WriteBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
      FS.WriteBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
    }  end; // for
    
    // produces rubish which isn't loaded: FS.WriteBuffer(ARMap, SizeOf(ARMap));
    
    // just one rec:
       FS.WriteBuffer(ARMap[0], SizeOf(TRMap) * REC_COUNT);
    // just one rec: FS.WriteBuffer(Pointer(ARMap)^, Length(ARMap)* SizeOf(Integer));
    
     FS.Free;
     ARMap := nil;
    end;
    
    
    procedure TForm1.LoadMapClick(Sender: TObject);
    var
    i, FileSize: Integer;
    begin
    try
        FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.map', fmOpenRead);
     //   FS := TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'test.dat', fmOpenRead);
      except
        ShowMessage('Couldn''t open file.');
        Exit;
      end;
         FS.ReadBuffer(FileSize, SizeOf(FileSize));
    //     Fs.ReadBuffer(FileSize, FS.Size div SizeOf(Integer));
         SetLength(ARMap, FileSize);
         FS.ReadBuffer(ARMap[0], SizeOf(TRMap) * FileSize);
    
      Listbox1.Clear;
      for i := 0 to FileSize-1 do
      begin
    { is working as well, but more mess
      FS.ReadBuffer(ARMap[i].X, SizeOf(ARMap[i].X));
      FS.ReadBuffer(ARMap[i].Y, SizeOf(ARMap[i].Y));
      FS.ReadBuffer(ARMap[i].Index, SizeOf(ARMap[i].Index));
    }
    
      with Listbox1.Items do
      begin
      Add(' ');
      Add(' *** Record NO: ' + InttoStr(i));
      Add(' ');
      Add('ARMap['+ InttoStr(i) +'].X: ' + InttoStr(ARMap[i].X));
      Add('ARMap['+ InttoStr(i) +'].Y: ' + InttoStr(ARMap[i].Y));
      Add('ARMap['+ InttoStr(i) +'].Index: ' + InttoStr(ARMap[i].Index));
      end; // with
      end; // for
     FS.Free;
    end;
    
    end.
    Last edited by WILL; 07-11-2010 at 07:23 PM. Reason: Please use code blocks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •