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 2 von 2     12   
Der schöne Günther
Online

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

AW: Update/Delete Record items in a Stream

  Alt 27. Apr 2018, 17:00
I do get you, and I understand what you want. It's just my personal opinion: I believe there is absolutely no use of putting this much work into all of this when everything you need is already there. It makes no sense to not read and write a few bytes.

Anyway , the part above ("Seeking and Positioning") is important as well. When you only want to read the LastName part of your file, you will have to skip the FirstName part. Since you do not know how many bytes are used to store the FirstName, you will first have to read the length of FirstName, then use Stream.Seek(..) from your current position to reach the part where the LastName string is stored.

I'll give you two examples:
The easiest one first:
Delphi-Quellcode:
function loadLastNameFromStream_EASY(): String;
var
   LoadingStream: TFileStream;
begin
   LoadingStream := TFileStream.Create('SAVE.test', fmOpenRead or fmShareDenyWrite);
   try
      // read it, but do nothing with it
      ReadStringFromStream(LoadingStream);
      Result := ReadStringFromStream(LoadingStream);
   finally
      LoadingStream.Destroy();
   end;
end;

The harder part is essentially the same. The "advantage" is not reading the UTF8 text bytes of FirstName . The disadvantage is that you end up more complicated code:
Delphi-Quellcode:
function loadLastNameFromStream_HARDER(): String;
var
   LoadingStream: TFileStream;
   stringLength: Integer;
begin
   LoadingStream := TFileStream.Create('SAVE.test', fmOpenRead or fmShareDenyWrite);
   try
      // determine length of FirstName
      LoadingStream.ReadBuffer(stringLength, SizeOf(stringLength));
      // skip
      LoadingStream.Seek(stringLength, TSeekOrigin.soCurrent);
      // we have now reached the position where "LastName" is stored, proceed as usual
      Result := ReadStringFromStream(LoadingStream);
   finally
      LoadingStream.Destroy();
   end;
end;

Once again: I believe you're taking the wrong approach. We have now a way of just reading the "LastName" part from a file. The way of writing the LastName is much more complicated: You can't just put a few additional bytes into a file and keep the rest as it is. When you change the LastName to something longer, you will also overwrite the ID part in your file. If you change the name to something shorter, garbage data will remain between the end of LastName and ID .
To avoid corruption, you will first have to read everything that comes after LastName, then seek back to where LastName starts, then write your LastName part, and then write everything else you previously read in. Then, in case your new name is shorter, truncate the file so no garbage data remains at the end.

Is all of this worth the hassle? No, it's not.
You have a handy method of reading a TMyRecord, and writing one as well. Just use them and use all the time saved for something nice.

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

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

AW: Update/Delete Record items in a Stream

  Alt 28. Apr 2018, 09:13
So many thanks for all your great efforts .
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


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 11:48 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