TStringsEncoding (C++)

From RAD Studio Code Examples
Jump to: navigation, search


This example requires a TMemo control, two buttons and two textfields on the form. It demonstrates the new TStrings.Encoding property and the overloads of TStrings.LoadFromFile and SaveToFile that use it. Use this example to save lines in a specific encoding to a file and then load the file back in, reading the encoding. Notice that the ASCII format does not preserve wide characters.


void __fastcall TForm1::Button1Click(TObject *Sender)
  // You may need to change this path to suit your environment.
  String Path = "../../"+Edit1->Text;
  if (Memo1->Lines->Encoding != NULL)
	Edit2->Text = Memo1->Lines->Encoding->EncodingName;
  Memo1->ScrollBars = ssVertical;
void __fastcall TForm1::Button2Click(TObject *Sender)
  // You may need to change this path to suit your environment.
  TEncoding *myEncoding;
  String Path = "../../"+Edit1->Text;
  myEncoding = TEncoding::Default;
  if (Edit2->Text == "ASCII") myEncoding = TEncoding::ASCII;
  if (Edit2->Text == "BigEndianUnicode") myEncoding = TEncoding::BigEndianUnicode;
  if (Edit2->Text == "Default") myEncoding = TEncoding::Default;
  if (Edit2->Text == "Unicode") myEncoding = TEncoding::Unicode;
// Do not use UTF7 for this.  It does not have a BOM, and so the encoding cannot be detected on a load.
//  if (Edit2->Text == "UTF7") then myEncoding = TEncoding::UTF7;
  if (Edit2->Text == "UTF8") myEncoding = TEncoding::UTF8;
  Memo1->Lines->SaveToFile(Path, myEncoding);