AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Update/Delete Record items in a Stream

Ein Thema von mandoza · begonnen am 27. Apr 2018 · letzter Beitrag vom 28. Apr 2018
Antwort Antwort
Seite 1 von 2  1 2      
mandoza

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

Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 10: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 10:52 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.541 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:01
Why not define a specialized list?
Delphi-Quellcode:
uses ..., System.Generics.Collections;

type
  TMyRecList = class(TList<Tmyrecord>)
  public
    procedure LoadFromFile(const Filename: string);
    procedure SaveToFile(const Filename: string);
  end;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Der schöne Günther

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:08
And then your next steps will probably be writing UpdateFirstName or UpdateId methods.

Why not
  1. Load your data from stream
  2. Alter them in memory
  3. Save everything again

It looks like you already have everything you need.
  Mit Zitat antworten Zitat
mandoza

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:09
Why not define a specialized list?
Delphi-Quellcode:
uses ..., System.Generics.Collections;

type
  TMyRecList = class(TList<Tmyrecord>)
  public
    procedure LoadFromFile(const Filename: string);
    procedure SaveToFile(const Filename: string);
  end;
Thank you , but what i wanted is to be able to update / delete items from my record then save / read again .
please read the

Delphi-Quellcode:
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;
  Mit Zitat antworten Zitat
mandoza

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:10
And then your next steps will probably be writing UpdateFirstName or UpdateId methods.

Why not
  1. Load your data from stream
  2. Alter them in memory
  3. Save everything again

It looks like you already have everything you need.
How to do that please ?
  Mit Zitat antworten Zitat
mandoza

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:41
what i wanted is to be able to :
1- Load that record item from Stream ( Done )
2- Update that record item value ( Done )
3- Update that modified record item value in the Stream , that means Replace that OLD item value with this New Value then save again ( HOW TO )
  Mit Zitat antworten Zitat
Der schöne Günther

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:47
Can you post your real code? Your current LoadFromStream() and SaveToStream() appear to be testing routines because they contain syntax errors and, in their current form, do not make sense:

In LoadFromStream() you load your data into a local variable myRec . After that, your procedure ends and myRec is gone. You can inspect myRec in the debugger, but in the end, this method accomplishes nothing.

"Real code" would, for example, take an instance of TMyrecord and a filename as parameters for LoadFromStream() and SaveToStream()
  Mit Zitat antworten Zitat
mandoza

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 11:55
Can you post your real code? Your current LoadFromStream() and SaveToStream() appear to be testing routines because they contain syntax errors and, in their current form, do not make sense:

In LoadFromStream() you load your data into a local variable myRec . After that, your procedure ends and myRec is gone. You can inspect myRec in the debugger, but in the end, this method accomplishes nothing.

"Real code" would, for example, take an instance of TMyrecord and a filename as parameters for LoadFromStream() and SaveToStream()
Thank you , i don't have an issue with LoadFromStream() and SaveToStream() , because i'll use my Rec else where , my issue now is with updating MyRec value then save it again ( at same stream position ) . thank you again

Delphi-Quellcode:
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 := ReadStringFromStream(SavingStream); // load the OLD value
       myRec.LastName:=NewLastName; // update it
      
      {
    Here how to update myRec.LastName value in the LoadingStream
      }

    
  finally
    SavingStream.Free;
  end;
end;

Geändert von mandoza (27. Apr 2018 um 11:59 Uhr)
  Mit Zitat antworten Zitat
Der schöne Günther

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
mandoza

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 13:51
many thanks for your great efforts , is there any other alternative to load just one field say :

myRec.LastName := ReadStringFromStream(LoadingStream); let's update myRec.LastName with a new value
myRec.LastName := 'Wombat'; // to change
not the whole record

then save only this new field value , something like update then save whithout loading the whole record just the target one .
I hope you get me now .

like this step :
https://www.thoughtco.com/create-dat...-files-1058003

please read the "Change and Update" part

Geändert von mandoza (27. Apr 2018 um 13:58 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:53 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz