Einzelnen Beitrag anzeigen

mandoza

Registriert seit: 27. Apr 2018
16 Beiträge
 
#1

Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:48
Delphi-Version: 10.2 Tokyo
Hi every one , i save my records into a stream i can read them back , but what i want is to be able to update or delete one of my records item then save back .
please how to do that . thank you


Delphi-Quellcode:
Tmyrecord = Record
  Firstname : string ;
  LastName:string;
  ID : integer ;
end;

....

procedure WriteIntegerToStream(Stream: TStream; Value: Integer);
begin
  Stream.WriteBuffer(Value, Sizeof(Value));
end;

procedure WriteStringToStream(Stream: TStream; const Value: String);
var
  S: UTF8String;
  Len: Integer;
begin
  S := UTF8String(Value);
  Len := Length(S);
  Stream.WriteBuffer(Len, Sizeof(Len));
  Stream.WriteBuffer(PAnsiChar(S)^, Len);
end;

function ReadIntegerFromStream(Stream: TStream): Integer;
begin
  Stream.ReadBuffer(Result, Sizeof(Result));
end;

function ReadStringFromStream(Stream: TStream): String;
var
  S: UTF8String;
  Len: Integer;
begin
  Stream.ReadBuffer(Len, Sizeof(Len));
  SetLength(S, Len);
  Stream.ReadBuffer(PAnsiChar(S)^, Len);
  Result := String(S);
end;

Procedure SaveToStream();
var
  SavingStream: TFileStream;
  i, j: Integer;
  myRec:Tmyrecord;
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
  LoadingStream: TFileStream;
  i, j: Integer;
  myRec:Tmyrecord;
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;

procedure UpdateMyRecLastName(NewLastName:string);
var
  LoadingStream: TFileStream;
  i, j: Integer;
  myRec:Tmyrecord;
begin
  SavingStream := TFileStream.Create('SAVE.test', fmCreate or fmOpenWrite or fmShareDenyWrite);
  try
      
       myRec.LastName:=NewLastName;
      
      {
     Here how to update myRec.LastName value in the LoadingStream
      }

    
  finally
    SavingStream.Free;
  end;
end;

procedure DeleteMyRecLastName();
var
  LoadingStream: TFileStream;
  i, j: Integer;
  myRec:Tmyrecord;
begin
  SavingStream := TFileStream.Create('SAVE.test', fmCreate or fmOpenWrite or fmShareDenyWrite);
  try
      {
     Here how to Delete myRec.LastName value in the LoadingStream
      }

    
  finally
    SavingStream.Free;
  end;
end;

Geändert von mandoza (27. Apr 2018 um 11:52 Uhr)
  Mit Zitat antworten Zitat