Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Tiburon’s LoadFromFile and SaveToFile for Unicode characters

Author: Markus Humm

With Tiburon, I can use Unicode characters with VCL components like TMemo, TListBox, TComboBox (and others that contain string lists).  How can I  load the strings from a file and save the strings to a file? How do I need to modify any existing Delphi and C++Builder programs to handle Unicode characters for these components?  Here is the answer.

There is a new, optional, parameter for the LoadFromFile and SaveToFile methods. The optional parameter is named “Encoding” and its type is class type “TEncoding”. TEncoding (defined in the SysUtils unit) contains several class properties that you can use to specify the type of strings you want to load and/or save:  ASCII, BigEndianUnicode, Default, Unicode, UTF7, UTF8.

The following are the declarations for LoadFromFile and SaveToFile methods for components that contain TStrings (defined in the Classes unit)

Delphi:
  procedure TStrings.LoadFromFile(const FileName: string);
  procedure TStrings.LoadFromFile(const FileName: string; Encoding: TEncoding);
  procedure TStrings.SaveToFile(const FileName: string);
  procedure TStrings.SaveToFile(const FileName: string; Encoding: TEncoding);

C++Builder:
  virtual void __fastcall LoadFromFile(const System::UnicodeString FileName)/* overload */;
  virtual void __fastcall LoadFromFile(const System::UnicodeString FileName, Sysutils::TEncoding* Encoding)/* overload */;
  virtual void __fastcall SaveToFile(const System::UnicodeString FileName)/* overload */;
  virtual void __fastcall SaveToFile(const System::UnicodeString FileName, Sysutils::TEncoding* Encoding)/* overload */;

Looking at the Delphi implementation for SaveToFile shows the use of TStream and the encoding I provide:

procedure TStrings.SaveToFile(const FileName: string);
begin
  SaveToFile(FileName, nil);
end;

procedure TStrings.SaveToFile(const FileName: string; Encoding: TEncoding);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    SaveToStream(Stream, Encoding);
  finally
    Stream.Free;
  end;
end;

The following examples show how to load and save the strings with a ListBox VCL component on your form:

Delphi:
  Listbox1.Items.LoadFromFile(‘c:\temp\MyListBoxItems.txt’,TEncoding.UTF8)
  ListBox1.Items.SaveToFile(‘MyListBoxItems.txt’,TEncoding.UTF8);

C++Builder:
  ListBox1->Items->LoadFromFile(“c:\\temp\\MyListBoxItems.txt”, TEncoding::UTF8);
  ListBox1->Items->SaveToFile(“c:\\temp\\MyListBoxItems.txt”,TEncoding::UTF8);

Here is a screen shot of my example Delphi application:

 

Here are links to the Delphi and C++Builder versions of the application:  delphihelloworld_660.zip  cpphelloworld_661.zip

With Tiburon, now my Delphi and C++ demo applications can handle Unicode characters in list boxes, edit boxes, and labels,  and I can also save and load the Unicode strings to/from my hard drive.

Exit mobile version