Solved

Delphi - Reading a textfile - Unicode issues?

Posted on 2011-09-15
6
488 Views
Last Modified: 2012-05-12
Hello,

I am trying to read a small textfile into a string... but when i do it with the code below I just geta load of junk.

I suspect unicode issues.  The textfile is written in Notepad, and I am using Delphi RAD 2010.

Is there a better way to do this?
TemplateFilename := extractfilepath( Application.ExeName ) + 'Templates\' + MailTemplateFilename ;

    fsBody := TFileStream.Create( TemplateFilename, fmOpenRead );
    SetLength(Body, fsBody.Size);
    fsBody.Seek( 0, soFromBeginning);
    fsBody.ReadBuffer(Body[1], fsBody.Size);

    showmessage( body );

Open in new window

0
Comment
Question by:soozh
6 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 36541578
Try using readln instead of readbuffer

var
   TemplateFilename : TextFile;
   buffer : string;
 begin
   AssignFile(TemplateFilename, extractfilepath( Application.ExeName ) + 'Templates\' + MailTemplateFilename) ;
   Reset(TemplateFilename) ;
   while not EOF(TemplateFilename) do
   begin
    ReadLn(TemplateFilename, buffer) ;
    ShowMessage(buffer) ;
   end;
   CloseFile(TemplateFilename) ;
 end;
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36541584
or take the size of a char into account

fsBody.ReadBuffer(Body[1], fsBody.Size * Sizeof(Char) );

Open in new window

0
 
LVL 3

Expert Comment

by:VahaC
ID: 36541586
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 500 total points
ID: 36541591
Or just load it into a TStringList

Var
  List: Tstrings;
begin
  List := TStringList.Create;
  try
    List.LoadFromFile(extractfilepath(Application.ExeName) + 'Templates\' + MailTemplateFilename);
    ShowMessage(List.Text);
  finally
    List.Free;
  end;
end;
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36541917
one note about the fix I gave you : for this to work you'll have to be sure that the file is encoded as unicode. That illustrates the real reason why your code is  faulty, and provides the way to fix those kind of errors (reading strings from streams, be there files or other streams)

Well now, in your case readln or TStringList.LoadFromFile are better solutions .
I just hope that does not mean changing too much of your code to implement them. For sure, next time you do such things, use those techniques directly as they can manage more combinations of "ANSI/Unicode files" and "ANSI/Unicode String type - so Delphi version"
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 36542141
why not keep it simple to see the file in delphi program :

procedure TForm1.BtnLoadClick(Sender: TObject);
begin
  memo1.Lines.LoadFromFile(YourFileName);
end;


but ... one other thing
if you work in windows environment for example russian: MSWIN1251 character set
and save that file in a ascii file

and then reopen that same file in a europe windows environiment MSWIN1252 character set
then you will also see junk.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now