Lately, I had a simple task to save and load some short strings in file.
The quickest and fastest way is to do it with streams (TFileStream or TMemoryStream).
Because in Delphi 2009 string type strings are Unicode so they are also double byte sized than in previous Delphi strings.
So you need to be very strict to write exact BYTES of the string, not characters, because character size is now 2 Bytes!
For Writing String to any stream use these Functions :
procedure WriteStreamInt(Stream : TStream; Num : integer); begin Stream.WriteBuffer(Num, SizeOf(Integer)); end; procedure WriteStreamStr(Stream : TStream; Str : string); var StrLen : integer; begin StrLen := Length(Str); //All Delphi versions compatible WriteStreamInt(Stream, StrLen); Stream.WriteBuffer(Pointer(Str)^, StrLen * SizeOf(Char)); //All Delphi versions compatible end;
The reading of string shoud be also exact with character size in mind. Because you can read only half of string:
function ReadStreamInt(Stream : TStream) : integer; begin Stream.ReadBuffer(Result, SizeOf(Integer)); end; function ReadStreamStr(Stream : TStream) : string; var StrLen : integer; TempStr : string; begin TempStr := ''; StrLen := ReadStreamInt(Stream); if StrLen > -1 then begin SetLength(TempStr, StrLen); //Here you should also check the character size //Reading Bytes! Stream.ReadBuffer(Pointer(TempStr)^, StrLen * SizeOf(Char)); result := TempStr; end else Result := ''; end;
Then you can write functions for writing and loading strings for example:
function SaveToDFile( FileName: String; sDescr : string; ) : boolean; var MemStr: TMemoryStream; begin MemStr:= TMemoryStream.Create; try try WriteStreamStr( MemStr, sDescr ); MemStr.SaveToFile(FileName); result := True; except on E:EWriteError do result := False; end; finally MemStr.Free; end; end; function LoadFromDFile( FileName: String; var sDescr : string; ) : boolean; var MemStr: TMemoryStream; begin MemStr:= TMemoryStream.Create; try try MemStr.LoadFromFile(FileName); sDescr := ReadStreamStr( MemStr ); result := True; except on E:EReadError do result := False; end; finally MemStr.Free; end; end;
Hope it helps some.
February 8th, 2010 at 11:51 pm
Nice, but you forgot to include the code for writing to a stream
February 10th, 2010 at 10:13 am
Oh man, must be very distrait ..
Fixing it.
February 24th, 2010 at 11:52 am
nice tip:)
my favorite delphi components http://www.components4developers.com
July 9th, 2012 at 1:04 pm
On Delphi XE and XE2 it’s not working. The easiest way would be to use TStringStream, f.e:
March 30th, 2014 at 8:15 am
thank you your tip
but not work on Delphi XE2
How to … please help
December 11th, 2020 at 1:36 pm
Good post. I be taught something more difficult on totally different blogs everyday. It’ll always be stimulating to read content from different writers and follow a bit of one thing from their store. I抎 prefer to use some with the content on my blog whether you don抰 mind. Natually I抣l provide you with a hyperlink on your internet blog. Thanks for sharing.