Saving and Loading strings in stream the Delphi 2009 way

Refresh 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.

About markoze

I'm illustrator, artist and graphics designer. I also program occasionally for web or desktop projects. View all posts by markoze

6 responses to “Saving and Loading strings in stream the Delphi 2009 way

  • Bourgui

    Nice, but you forgot to include the code for writing to a stream

  • Carl

    nice tip:)

    my favorite delphi components http://www.components4developers.com

  • Pavel

    On Delphi XE and XE2 it’s not working. The easiest way would be to use TStringStream, f.e:

    var
      _MemStr: TStringStream;
    begin
      _MemStr:= TStringStream.Create('Some string');
      try
        // by your example
        WriteStreamStr( MemStr, sDescr ); // if u use not XE then ok, else delete it
        MemStr.SaveToFile(FileName);
    
        // if u want save to database using blob field type...  
        Parameters.FindParam('prm_object').LoadFromStream(_MemStr, ftBlob);
    
        // or what you want else for your decision for using stream -__^
      finally
        _MemStr.Free;
      end;
    
  • warapetch

    thank you your tip
    but not work on Delphi XE2
    How to … please help

  • Hairstyles

    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.

Leave a reply to Carl Cancel reply