![]() |
AW: Update/Delete Record items in a Stream
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
Delphi-Quellcode:
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
FirstName
![]() 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
Delphi-Quellcode:
. The disadvantage is that you end up more complicated code:
FirstName
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
Delphi-Quellcode:
and
LastName
Delphi-Quellcode:
.
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. :wink: |
AW: Update/Delete Record items in a Stream
So many thanks for all your great efforts .
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:56 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz