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

Fills the list with the lines of text in a specified file.

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

Call LoadFromFile to fill the list of the TStrings object from the file specified by FileName. LoadFromFile first clears any strings already in the list. As indicated by carriage return or linefeed characters, each line in the file is then appended in the list as a string. 

If the Encoding parameter is not given, then the strings are loaded using the appropriate encoding. The value of the encoding is obtained by calling the GetBufferEncoding routine of the TEncoding class.

Note: LoadFromFile uses the Add method to add the strings that are read from the file.
 

C++ Examples: 

 

/*
This example shows how to use the TOpenTextFileDialog and
TSaveTextFileDialog to handle files with specific encodings.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    openTxtDlg = new TOpenTextFileDialog(this);
    saveTxtDlg = new TSaveTextFileDialog(this);

    // create a new string list that holds encoding info
    TStrings* encodings = new TStringList();

    // add some encodings to the list
    encodings->AddObject("ASCII", TEncoding::ASCII);
    encodings->AddObject("UNICODE", TEncoding::Unicode);
    encodings->AddObject("UTF8", TEncoding::UTF8);

    openTxtDlg->Encodings->Assign(encodings);
    saveTxtDlg->Encodings->Assign(encodings);

    openTxtDlg->Filter = "Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*";
    saveTxtDlg->Filter = "Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*";

}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (openTxtDlg->Execute(this->Handle))
    {
        // selecting the filename and encoding selected by the user
        String filename = openTxtDlg->FileName;

        int encIndex = openTxtDlg->EncodingIndex;

        TEncoding* enc = dynamic_cast<TEncoding*>
            (openTxtDlg->Encodings->Objects[encIndex]);

        // checking if the file exists
        if (FileExists(filename))
        {
            // display the contents in a memo based on the selected encoding
            Memo1->Lines->LoadFromFile(filename, enc);
        }
        else
        {
            MessageDlg("File does not exist", mtError,
                TMsgDlgButtons() << mbOK, 0);
        }
    }
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (saveTxtDlg->Execute(this->Handle))
    {
        // selecting the filename and encoding selected by the user
        String filename = saveTxtDlg->FileName;

        int encIndex = saveTxtDlg->EncodingIndex;

        TEncoding* enc = dynamic_cast<TEncoding*>
            (saveTxtDlg->Encodings->Objects[encIndex]);

        // checking if the file exists
        if (FileExists(filename))
        {
            MessageDlg("File already exists", mtError,
                TMsgDlgButtons() << mbOK, 0);
        }
        else
        {
            // saves to file based on the selected encoding.
            Memo1->Lines->SaveToFile(filename, enc);
        }

    }
}
/*
This example requires two TRichEdit controls placed on the
form.  When the form becomes visible, the first Rich Edit
control will display the rich text in its raw form, including
formatting characters. The second will show the rich text in
its intended format.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// you may need to change this path to suit your environment
  char const *Path = "..\\OVERVIEW.RTF";
  RichEdit1->PlainText = true;
  RichEdit1->Lines->LoadFromFile(Path);
  RichEdit2->PlainText = false;
  RichEdit2->Lines->LoadFromFile(Path);
}

 

Delphi Examples: 

{
This example shows how to use the TOpenTextFileDialog and
TSaveTextFileDialog to handle files with specific encodings.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  Encoding : TEncoding;
  EncIndex : Integer;
  Filename : String;
begin
  if OpenTxtDlg.Execute(Self.Handle) then
    begin
    //selecting the filename and encoding selected by the user
    Filename := OpenTxtDlg.FileName;

    EncIndex := OpenTxtDlg.EncodingIndex;
    Encoding := OpenTxtDlg.Encodings.Objects[EncIndex] as TEncoding;

    //checking if the file exists
    if FileExists(Filename) then
      //display the contents in a memo based on the selected encoding
      Memo1.Lines.LoadFromFile(FileName, Encoding)
    else
      raise Exception.Create('File does not exist.');
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Encoding : TEncoding;
  EncIndex : Integer;
  Filename : String;
begin
  if SaveTxtDlg.Execute(Self.Handle) then
    begin
    //selecting the filename and encoding selected by the user
    Filename := SaveTxtDlg.FileName;

    EncIndex := SaveTxtDlg.EncodingIndex;
    Encoding := SaveTxtDlg.Encodings.Objects[EncIndex] as TEncoding;

    //checking if the file exists
    if FileExists(Filename) then
      raise Exception.Create('File already exists.')
    else
      //save to file based on the selected encoding
      Memo1.Lines.LoadFromFile(FileName, Encoding);
    end;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  encodings : TStrings;
begin
  OpenTxtDlg := TOpenTextFileDialog.Create(Self);
  SaveTxtDlg := TSaveTextFileDialog.Create(Self);

  // create a new string list that holds encoding info
  encodings := TStringList.Create();

  // add some encodings to the list
  encodings.AddObject('ASCII', TEncoding.ASCII);
  encodings.AddObject('UNICODE', TEncoding.Unicode);
  encodings.AddObject('UTF8', TEncoding.UTF8);

  OpenTxtDlg.Encodings.Assign(encodings);
  SaveTxtDlg.Encodings.Assign(encodings);

  OpenTxtDlg.Filter := 'Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*';
  SaveTxtDlg.Filter := 'Text files (*.txt)|*.TXT|XML files (*.xml)|*.XML|Any file (*.*)|*.*';

end;
{
This example requires two TRichEdit controls placed on the 
form.  When the form becomes visible, the first Rich Edit 
control will display the rich text in its raw form, including
formatting characters. The second will show the rich text in 
its intended format.
}

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

 

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