Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Write Unicode using System.TextFile file?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Nov 19, 2015 11:07 AM Last Post By: Peter Below
Philip Kapusta

Posts: 2
Registered: 3/19/04
Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 5:33 AM
Is it possible using Delphi 2010 to use System.TextFile to save a TString in Unicode format? Currently the following code fails to save the TString as Unicode.

var
FileHandle : System.TextFile;
begin
System.Assign(FileHandle, 'testing123');
ReWrite(FileHandle);
WriteLn(FileHandle, AUnicodeString);
System.Close(FileHandle);

Yes, I do know how to use a TStringList and its method .SaveToFile to create a Unicode file, but my question pertains to System.TextFile.

Thank you.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 6:51 AM   in response to: Philip Kapusta in response to: Philip Kapusta
You can create a Unicode files using TextFile in newer versions od
Delphi. I have no access to Delphi 2010 in order to verify whenever
Assignfile already has the third parameter for code page or it was
introduced in versions after.

In XE5 you can create an UTF-8 using the following code:

var
  FileHandle : System.TextFile;
  s: string;
begin
  s:='Test Тест';
  System.Assign(FileHandle, 'testing123.txt', 65001);
  ReWrite(FileHandle);
  WriteLn(FileHandle, s);
  System.Close(FileHandle);
end;


Or UTF-16 using code page 1200:

var
  FileHandle : System.TextFile;
  s: string;
begin
  s:='Test Тест';
  System.Assign(FileHandle, 'd:\temp\testing123.txt', 1200);
  ReWrite(FileHandle);
  WriteLn(FileHandle, s);
  System.Close(FileHandle);
end;
Colton Morrison

Posts: 2
Registered: 7/22/12
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 6:57 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos Juhasz wrote:
  System.Assign(FileHandle, 'testing123.txt', 65001);

The additional 65001 parameter in System.Assign is not supported in Delphi 2010.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 7:40 AM   in response to: Colton Morrison in response to: Colton Morrison
Colton Morrison wrote:

Lajos Juhasz wrote:
  System.Assign(FileHandle, 'testing123.txt', 65001);

The additional 65001 parameter in System.Assign is not supported in
Delphi 2010.

A searched further looks like you can do that from XE2
http://docwiki.embarcadero.com/Libraries/XE2/en/System.AssignFile.

You can still create a text file using File of byte - not TextFile as
you want to do and BlockWrite instead of WriteLn (this is also XE5 code
as I have no access to D2010):

var
  FileHandle : File Of Byte;
  s: string;
  ar: TBytes;
begin
  s:='Test Тест'+#13#10;
  System.Assign(FileHandle, 'testing123.txt');
  ReWrite(FileHandle);
  ar:=TEncoding.UTF8.GetBytes(s);
  System.BlockWrite(FileHandle, ar[0], length(ar));
  System.Close(FileHandle);
  close;
end;


For creating unicode files I believe it would be better to use
Tstrings.SaveToFile or to use TFileStream.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 10:55 AM   in response to: Colton Morrison in response to: Colton Morrison
Colton wrote:

The additional 65001 parameter in System.Assign is not supported
in Delphi 2010.

The codepage parameter was added in XE2.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 11:00 AM   in response to: Philip Kapusta in response to: Philip Kapusta
Philip wrote:

Is it possible using Delphi 2010 to use System.TextFile to save
a TString in Unicode format?

Not in Delphi 2010, no. That feature was added in XE2. You will have to
use a different approach, such as TStreamWriter:

var
  Writer : TStreamWriter;
begin
  Writer := TStreamWriter.Create('testing123', false, TEncoding.UTF8); // 
or whichever encoding you want...
  try
    Writer.WriteLine(AUnicodeString);
  finally
    Writer.Free;
  end;
end;


Yes, I do know how to use a TStringList and its method .SaveToFile to
create a Unicode file, but my question pertains to System.TextFile.

Why do you want to use old-style Pascal File I/O when there are several
modern alternatives available? TStringList, TStreamWriter, even FileCreate/FileWrite().

--
Remy Lebeau (TeamB)
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Write Unicode using System.TextFile file?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 19, 2015 11:07 AM   in response to: Philip Kapusta in response to: Philip Kapusta
Philip Kapusta wrote:

Is it possible using Delphi 2010 to use System.TextFile to save a
TString in Unicode format? Currently the following code fails to
save the TString as Unicode.

var
FileHandle : System.TextFile;
begin
System.Assign(FileHandle, 'testing123');
ReWrite(FileHandle);
WriteLn(FileHandle, AUnicodeString);
System.Close(FileHandle);

Yes, I do know how to use a TStringList and its method .SaveToFile to
create a Unicode file, but my question pertains to System.TextFile.

Thank you.

I suggest you convert your code to use a TStreamWriter instead. The old
Textfile I/O is so 80s...

--
Peter Below
TeamB
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02