RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.SaveToFile Method (string)

Saves the strings in the list to the specified file.

Pascal
procedure SaveToFile(const FileName: string); virtual; overload;
procedure SaveToFile(const FileName: string; Encoding: TEncoding); virtual; overload;
C++
virtual __fastcall SaveToFile(const AnsiString FileName);
virtual __fastcall SaveToFile(const AnsiString FileName, TEncoding Encoding);

Call SaveToFile to save the strings in the list to the file specified by FileName. Each string in the list is written to a separate line in the file. 

If the Encoding parameter is not given, then the strings are saved with the default encoding, as specified in the Default property of the TEncoding class.

Note: In Linux, SaveToFile saves the strings as they appear in the list. If the last string does not have an end-of-line marker, the resulting file may be considered incomplete by some editors.
 

C++ Examples: 

 

/*
This example displays a print dialog box with a Print to
File check box, and then prints the contents of a rich edit
control to the indicated destination:
Verify that the file is saved by the SaveDialog.
*/
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  PrintDialog1->Options << poPrintToFile;
  PrintDialog1->PrintToFile = true;
  if (PrintDialog1->Execute())
  {
    if (PrintDialog1->PrintToFile)
    {
      SaveDialog1->Title = "Print to File:";

      /* Save in UTF8 format */
      if (SaveDialog1->Execute())
        RichEdit1->Lines->SaveToFile(SaveDialog1->FileName, TEncoding::UTF8);
      else
        RichEdit1->Print("");
    }
  }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
// you may need to change this path to suit your environment
  char const *Path = "..\\overview.rtf";
  RichEdit1->PlainText = false;

  /* Load from UTF8 format */
  RichEdit1->Lines->LoadFromFile(Path, TEncoding::UTF8);
  RichEdit1->ScrollBars = ssVertical;
}

 

Delphi Examples: 

{
This example displays a print dialog box with a Print to
File check box, and then prints the contents of a rich edit
control to the indicated destination:
Verify that the file is saved by the SaveDialog.
}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
with PrintDialog1 do
  begin
  Options := [poPrintToFile];
  PrintToFile := True;
  if Execute then
    begin
    if PrintToFile then
      begin
      SaveDialog1.Title := 'Print to File: ';

      { Save in UTF8 format }
      if SaveDialog1.Execute then
        RichEdit1.Lines.SaveToFile(SaveDialog1.FileName, TEncoding.UTF8);
      end
    else
      RichEdit1.Print('');
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
const
  // you may need to change this path to suit your environment
  Path = 'OverView.RTF';
begin
  RichEdit1.PlainText := False;

  { Load from UTF8 format }
  RichEdit1.Lines.LoadFromFile(Path, TEncoding.UTF8);
  RichEdit1.ScrollBars := ssVertical;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!