Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

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)

  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);

  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);
  SaveToFile(FileName, nil);

procedure TStrings.SaveToFile(const FileName: string; Encoding: TEncoding);
  Stream: TStream;
  Stream := TFileStream.Create(FileName, fmCreate);
    SaveToStream(Stream, Encoding);

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


  ListBox1->Items->LoadFromFile(“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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.