Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting strings to TMemoryStream
    primarykey
    data
    text
    <p>I have read alot of discussions on here regarding writing strings to a TMemoryStream and saving to file and reading the strings back in to TMemoryStream</p> <p>I dont know what I have done wrong here, but either my SaveData or my LoadData is wrong. I can check the value of Title before I call SaveData and it holds what I expect. However, when I call LoadData and check the value of Title, it is garbage. Can anyone tell me what I have done wrong please</p> <pre><code>procedure SaveData(FileName: TFileName); var MemStr: TMemoryStream; Title: String; begin MemStr:= TMemoryStream.Create; try MemStr.Seek(0, soFromBeginning); WriteStreamStr( MemStr, TItle ); MemStr.SaveToFile(FileName); finally MemStr.Free; end; end; procedure LoadData(FileName: TFileName); var MemStr: TMemoryStream; Title: String; begin MemStr:= TMemoryStream.Create; try MemStr.LoadFromFile(FileName); MemStr.Seek(0, soFromBeginning); Title := ReadStreamStr( MemStr ); finally MemStr.Free; end; end; procedure WriteStreamInt(Stream : TStream; Num : integer); {writes an integer to the stream} begin Stream.WriteBuffer(Num, SizeOf(Integer)); end; procedure WriteStreamStr(Stream : TStream; Str : string); {writes a string to the stream} var StrLen : integer; begin {get length of string} StrLen := Length(Str); {write length of string} WriteStreamInt(Stream, StrLen); if StrLen &gt; 0 then {write characters} Stream.Write(Str[1], StrLen); end; function ReadStreamInt(Stream : TStream) : integer; {returns an integer from stream} begin Stream.ReadBuffer(Result, SizeOf(Integer)); end; function ReadStreamStr(Stream : TStream) : string; {returns a string from the stream} var LenStr : integer; begin Result := ''; {get length of string} LenStr := ReadStreamInt(Stream); {set string to get memory} SetLength(Result, LenStr); {read characters} Stream.Read(Result[1], LenStr); end; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload