Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi - Reading a textfile - Unicode issues?

Posted on 2011-09-15
6
Medium Priority
?
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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