Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi - Reading a textfile - Unicode issues?

Posted on 2011-09-15
6
Medium Priority
?
496 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 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 2000 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 38

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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