Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Saving and reading text files in UTF8 / Unicode?

29 views
Skip to first unread message

itibira

unread,
Mar 3, 2006, 1:42:00 AM3/3/06
to
Greetings,

i have an application in D7 that uses the TnTRichEdit component. This
component was originally a TRichEdit and i was able to save and read
standard (ASCII) text into a binary file. With the new TnTRichEdit, i
am able to copy and paste, for example, Chinese characters into the
RichEdit component and it works OK.

However, i now need to save the Chinese characters into the binary file
and then, later, reload the text into the TnTRichEdit. My code for the
original ASCII text save is:

procedure TMyForm.SaveRichEditTextFileClick(Sender: TObject);
var
i: Integer;
j: Byte;
dmyString: ShortString;
begin
...
if SaveDialog.Execute then
begin
AssignFile(MyTypeFile, FileName);
ReWrite(MyTypeFile,1);
{ .. do some blockwrites of other data...}
...
j := MyRichEdit.Lines.Count;
BlockWrite(MyTypeFile,j,sizeof(j));

for i := 0 to MyRichEdit.Lines.Count-1 do
begin
dmyString := MyRichEdit.Lines.Strings[i];
BlockWrite(MyTypeFile,dmyString,succ(length(dmyString)));
end;
...
{...do more blockwrites...}
...
CloseFile(MyTypeFile);
end;
end;

and for reading it is:

procedure TMyForm.OpenRichEditTextFile(FileName: string);
var
FileNameLength: Integer;
i: Integer;
j: Byte;
dmyString: ShortString;
SizeByte: Byte;
FileType: string;
begin
...
if FileExists(FileName) then
begin
MyRichEdit.PlainText := True;
MyRichEdit.Lines.Clear;
AssignFile(MyTypeFile, FName);
FileMode := fmOpenRead;
Reset(SpeakTypeFile,1);
...
{.. do some other blockreads...}
....
BlockRead(MyTypeFile,j,sizeof(j)); {number of text lines to
follow}
if j <>0 then
for i := 0 to j-1 do
begin
BlockRead(MyTypeFile,SizeByte,sizeof(SizeByte));
{Get length of string that follows}
SetLength(dmyString, SizeByte);
BlockRead(MyTypeFile,dmyString[1],SizeByte);
{Get the whole string at one go}
MyRichEdit.Lines.Add(dmyString);
end;
...
{... some more blockreads...}
CloseFile(MyTypeFile);
end;
end;

However, i have tried using WideString and AnsiString variables but
when i view the saved file in Word, all i see is boxes (i used to get
question marks (?) before i used the WideString variables)

i'm running D7 on a Win 2000 Pro system.

i am a complete newbie to Unicode, WideString, AnsiString, UTF8, etc.
and i'm wondering where i'm going wrong. To be honest, i'm completely
lost...

Can anyone please help? Can someone show an example of how to convert
my code to be able to read and save the Unicode strings?

Thanks to everyone in advance for any and all help on this.

Cheers.

Riki Wiki

unread,
Mar 4, 2006, 9:51:48 PM3/4/06
to
On 2 Mar 2006 22:42:00 -0800, itibira wrote:

> Thanks to everyone in advance for any and all help on this.

Hoi

This newsgroup do not officially exist, that is why there is so few
messages here. Rather use b.p.d.internationalization.win32. Further you
need to repost your message/question on the Borland news server to make
everybody see it.

Take a look here:
<http://tinyurl.com/8m5nw>
which links to
<http://delphi.wikicities.com/wiki/Delphi_Newsgroups>

itibira

unread,
Mar 5, 2006, 12:12:31 AM3/5/06
to
Hi Riki,

Thanks very much for the help. No wonder i wasn't getting anywhere....

i do appreciate your help.

Cheers.

0 new messages