Einzelnen Beitrag anzeigen

Der schöne Günther
Online

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#9

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 13:43
I still don't understand the problem. Let's assume you change your local variable myRec to be a parameter of both LoadFromStream(..) and SaveToStream(..). They both now look like this:

Delphi-Quellcode:
Procedure SaveToStream(const myRec:Tmyrecord);
var
  SavingStream: TFileStream;
begin
  SavingStream := TFileStream.Create('SAVE.test', fmCreate or fmOpenWrite or fmShareDenyWrite);
  try
     WriteStringToStream(SavingStream, myRec.Firstname);
     WriteStringToStream(SavingStream, myRec.LastName);
     WriteIntegerToStream(SavingStream, myRec.ID);
  finally
   SavingStream.Free;
  end;
end;

procedure LoadFromStream(var myRec: TMyRecord);
var
  LoadingStream: TFileStream;
begin
  LoadingStream := TFileStream.Create('SAVE.test', fmOpenRead or fmShareDenyWrite);
  try

     myRec.Firstname:= ReadStringFromStream(LoadingStream);
      myRec.LastName := ReadStringFromStream(LoadingStream);
     myRec.ID := ReadIntegerFromStream(LoadingStream);

  finally
   LoadingStream.Free;
  end;
end;
If you want to change the LastName field of some file from disk, then all you need to do is

Delphi-Quellcode:
var
   myRec: Tmyrecord;
begin
   LoadFromStream(myRec);
   myRec.LastName := 'Wombat';
   SaveToStream(myRec);
end;
The old file ("save.test") will get overwritten and the file will only contain the data stored in myRec .
  Mit Zitat antworten Zitat