Показать сообщение отдельно
  #27  
Старый 29.06.2019, 18:01
lmikle lmikle вне форума
Модератор
 
Регистрация: 17.04.2008
Сообщения: 7,645
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
По умолчанию

Вот класс для хранения всего этого добра в одном файле:
Код:
unit Unit2;

interface

uses
  Classes, SysUtils;

type
  TModel = class
  private
    procedure Init;
  public
    PrjName : String;
    Results : Array[1..6,1..19] Of Integer;
    Sources : Array[1..14] Of Integer;
    Enabled_12_14 : Boolean;

    constructor Create;
    constructor Load(AStream : TStream); overload;
    constructor Load(AFileName : String); overload;
    destructor Destroy; override;

    procedure LoadFRomFile(AFileName : String);
    procedure SaveToFile(AFileName : String);

    procedure LoadFromStream(AStream : TStream);
    procedure SaveToStream(AStream : TStream);
  end;

implementation

procedure TModel.Init;
var
  I, J : Integer;
begin
  PrjName := '';
  For I := Low(Sources) To High(Sources) Do
    Sources[i] := 0;
  For I := Low(Results) To High(Results) Do
    For J := Low(Results[i]) To High(Results[i]) Do
      Results[I,J] := 0;
  Enabled_12_14 := False;
end;

constructor TModel.Create;
begin
  inherited;
  Init;
end;

constructor TModel.Load(AStream : TStream);
begin
  inherited Create;
  Init;
  LoadFromStream(AStream);
end;

constructor TModel.Load(AFileName : String);
begin
  inherited Create;
  Init;
  LoadFromFile(AFileName);
end;

destructor TModel.Destroy;
begin
  inherited;
end;

procedure TModel.LoadFRomFile(AFileName : String);
var
  AStream : TFileStream;
begin
  AStream := TFileStream.Create(AFileName, fmOpenRead);
  Try
    LoadFromStream(AStream);
  Finally
    AStream.Free;
  End;
end;

procedure TModel.SaveToFile(AFileName : String);
var
  AStream : TFileStream;
begin
  AStream := TFileStream.Create(AFileName, fmCreate);
  Try
    SaveToStream(AStream);
  Finally
    AStream.Free;
  End;
end;

procedure TModel.LoadFromStream(AStream : TStream);
var
  I, J : Integer;
begin
  // Read project name
  AStream.ReadBuffer(I,SizeOf(Integer));
  SetLength(PrjName,I);
  AStream.ReadBuffer(PrjName[1],I*SizeOf(Char));

  // Read source data
  For I := Low(Sources) To High(Sources) Do
    AStream.ReadBuffer(Sources[i],SizeOf(Integer));
  AStream.ReadBuffer(Enabled_12_14,SizeOf(Boolean));

  // Read results
  For I := Low(Results) To High(Results) Do
    For J := Low(Results[i]) To High(Results[i]) Do
      AStream.ReadBuffer(Results[I,J],SizeOf(Integer));
end;

procedure TModel.SaveToStream(AStream : TStream);
var
  I, J : Integer;
begin
  // Write project name
  I := Length(PrjName);
  AStream.WriteBuffer(I,SizeOf(Integer));
  AStream.WriteBuffer(PrjName[1],I*SizeOf(Char));

  // Write source data
  For I := Low(Sources) To High(Sources) Do
    AStream.WriteBuffer(Sources[i],SizeOf(Integer));
  AStream.WriteBuffer(Enabled_12_14,SizeOf(Boolean));

  // Write results
  For I := Low(Results) To High(Results) Do
    For J := Low(Results[i]) To High(Results[i]) Do
      AStream.WriteBuffer(Results[I,J],SizeOf(Integer));
end;

end.
Ты не сказал какой тип данных, соотв. я для простоты поставил Integer.

Аот пример использования из программы:
Код:
uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  With TModel.Create Do
    begin
      PrjName := 'Project1';
      For I := Low(Sources) To High(Sources) Do
        Sources[i] := Random(10);
      SaveToFile('c:\work\projects\prj1.dat');
      Free;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  With TModel.Load('c:\work\projects\prj1.dat') Do
    begin
      ShowMessage(PrjName);
      Free;
    end;
end;

Код работы с компонентами сам напишешь.
Ответить с цитированием