RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
C++

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:

delphihelloworld_658.jpg 

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

Related posts
C++Delphi

Get Free Responsive Cross-Platform Login Screen Templates For Android And iOS

C++CodeDelphi

Learn To Build A Python GUI For Working With The Numpy Library In A Delphi Windows App

C++Code

Learn About Using Right Angle Brackets In This C++11 Feature For Windows Development

C++DelphiRAD Studio

Learn How To Use WinRT In Your Native Windows Delphi And C++ Builder Apps

Leave a Reply

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

IN THE ARTICLES