Delphi - Reading a textfile - Unicode issues?

Posted on 2011-09-15
Last Modified: 2012-05-12

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

Question by:soozh
LVL 22

Accepted Solution

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

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

Expert Comment

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

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

Open in new window


Expert Comment

ID: 36541586
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 22

Assisted Solution

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

  List: Tstrings;
  List := TStringList.Create;
    List.LoadFromFile(extractfilepath(Application.ExeName) + 'Templates\' + MailTemplateFilename);
LVL 25

Expert Comment

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"
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);

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.


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 68
FMX enumerated colours 2 118
can i put an image from resource into image tag ? 6 27
shape, triangle, dbctrlgrid 3 17
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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