Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-07

Reading a file that contains UTF-8 text

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

Sorry if this is not in the correct mailing list, but I thought, seeing that Lazarus can read .pas files or .lfm files that contain utf-8 text, you guys might know the answer.

What is the correct why of reading a text file that contains UTF-8 text?

I am using the ReadBlock() method, and for testing purposes a WriteBlock() method. When I check the results of the new file populated by WriteBlock() the text looks fine.

But if I use ReadBlock() and store the result in a String (AnsiString) and then writeln() the read data, the utf-8 text gets mangled. As shown below....

Any thoughts as to why?

===================
     Hint := '';
     Text := '?ALabel';
   end;
===================

Writeln() can handle the text as far as I can see, because if I assign that exact same text to a String variable and then writeln() that variable, the text looks fine in the console output.

I'm using Ubuntu 8.04, FPC 2.2.5 and Lazarus 0.9.27 (trunk).

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Graeme Geldenhuys
to Zaher Dirkey
Zaher Dirkey wrote
I always use TStream based objects Like TFileStream, and forbid to my coworkers from using AssignFile and ReadLn or WriteLn and other functions in the same family.

That made no difference, though the actual code looks clearner.

I then tried to view the file with mc (Midnight Commander), and I couldn't read the unicode characters, they only appears as dots. I then opened the file in gEdit (Gnome Editor) and when I tried to save it, gEdit complained and said it couldn't save the file using the encoding ISO-8859-15 (What????) and if I would like to save it as UTF-8 instead. I said yes.

Now my program reads the file just fine, and the writeln() output is also readable. So I have no idea what happened with the original data file to start with. Anyway, the problem is now solved.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email