Skip to content

Instantly share code, notes, and snippets.

@Pasquina
Created April 4, 2021 21:30
Show Gist options
  • Save Pasquina/df816a52aba9d006a31c7a191a9e0664 to your computer and use it in GitHub Desktop.
Save Pasquina/df816a52aba9d006a31c7a191a9e0664 to your computer and use it in GitHub Desktop.
Sample FileStream writing TMemo lines
{ Saving from TMemo.Lines does not honor the TrailingLineBreak option of
TStrings. }
procedure TfSavingMemoLines.aSaveLinesExecute(Sender: TObject);
var
LMode: Word; // in case we need to create the file
LFileStream: TFileStream; // handle the writing
begin
if not FileExists(LogFileName) then // test for file exists
LMode := fmCreate // not present need to create
else
LMode := fmOpenReadWrite; // present need to append
LFileStream := TFileStream.Create(LogFileName, LMode or fmShareDenyWrite); // open log file
try
LFileStream.Seek(0, soFromEnd); // make sure at the end of the existing file
mMain.Lines.SaveToStream(LFileStream, TEncoding.UTF8); // add the lines to the end
finally
LFileStream.Free; // return resources
end;
end;
@Pasquina
Copy link
Author

Pasquina commented Apr 4, 2021

Example of using TFileStream to save lines from a TMemo component. This method fails to honor the TrailingLineBreak option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment