Einzelnen Beitrag anzeigen

Benutzerbild von Sprint
Sprint

Registriert seit: 18. Aug 2004
Ort: Edewecht
712 Beiträge
 
Delphi 5 Professional
 
#2

Re: 2 dimensionalen Array in Datei speichern und wieder lade

  Alt 20. Mär 2005, 14:37
Delphi-Quellcode:
const
  IDENT_TMYARRAY = 'TMyArray';
  BUF_SIZE = 4096;
Delphi-Quellcode:
type
  TMyArray = array[1..12, 1..2] of String;
Delphi-Quellcode:
procedure SaveToFile(const MyArray: TMyArray; const FileName: String);
var
  Stream: TFileStream;
  Writer: TWriter;
  I, J: Integer;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    Writer := TWriter.Create(Stream, BUF_SIZE);
    try
      Writer.WriteSignature;
      Writer.WriteIdent(IDENT_TMYARRAY);
      for I := 1 to 12 do
        for J := 1 to 2 do
          Writer.WriteString(MyArray[I, J]);
    finally
      Writer.Free;
    end;
  finally
    Stream.Free;
  end;
end;
Delphi-Quellcode:
procedure LoadFromFile(var MyArray: TMyArray; const FileName: String);
var
  Stream: TFileStream;
  Reader: TReader;
  I, J: Integer;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    Reader := TReader.Create(Stream, BUF_SIZE);
    try
      Reader.ReadSignature;
      if Reader.ReadIdent <> IDENT_TMYARRAY then
        raise Exception.Create(FileName + ' ist keine gültige Datei vom Typ TMyArray.');
      for I := 1 to 12 do
        for J := 1 to 2 do
          MyArray[I, J] := Reader.ReadString;
    finally
      Reader.Free;
    end;
  finally
    Stream.Free;
  end;
end;
Ciao, Sprint.

"I don't know what I am doing, but I am sure I am having fun!"
  Mit Zitat antworten Zitat