Solved

Delphi - Reading a textfile - Unicode issues?

Posted on 2011-09-15
6
489 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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