In Tiburon, I can use Unicode characters in controls such as TMemo, TListBox, TComboBox (and other lists containing characters. How can we load and save strings from a file? What changes do I need to make in a program that has been written using Delphi and C ++ Builder to process Unicode characters for these components? The answer is as follows:
A new and optional parameter is displayed in the LoadFromFile and SaveToFile methods. The optional parameter name is "Encoding" and its type is "TEncoding ". TEncoding (this type is declared in the SysUtils unit) class properties, which can help you specify the loading or saving strings types: ASCII, BigEndianUnicode, Default, Unicode, UTF7, UTF8.
The following is the declaration of LoadFormFile and SaveToFile in controls containing the TStrings type (declared 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 );
Virtual void _ fastcall LoadFromFile (const System: UnicodeString FileName, Sysutils: TEncoding * Encoding );
Virtual void _ fastcall SaveToFile (const System: UnicodeString FileName );
Virtual void _ fastcall SaveToFile (const System: UnicodeString FileName, Sysutils: TEncoding * Encoding );
View the SaveToFile method of implementation in Delphi. You can see that TStream is used to save the strings with TEncoding information:
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 example shows how to use The ListBox control to load and save strings 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 the screen of the above Delphi example program:
Using Tiburon, now my Delphi and C ++ demo programs can use uincode characters on the list box, edit box, and label, and I can also read and write Unicode strings directly on the hard disk.
Address: http://blogs.codegear.com/davidi/2008/07/15/38898/
Translation: Yu feiying
Reprinted please indicate the source!