Solved

How do I work with UTF-8 formatted files?

Posted on 2008-06-17
3
4,570 Views
Last Modified: 2013-11-23
In Delphi 7 I need to open a utf-8 encoded file, replace a token [%FILE%] with a string value, and save the modified file.  

I guess that I need to use a filestream, and a widestring, but I can't see an example of how to do it, and whether or not StringReplace will work, and whether I need to somehow convert the token I'm searching for to utf-8 format?  

Any pointers or code samples would be brilliant!  
0
Comment
Question by:DelphiMan7
3 Comments
 
LVL 28

Accepted Solution

by:
ciuly earned 125 total points
ID: 21802419
actually, you can use a normal textfile. read the string up (line by line for ex) and use utf8decode to decode the string. do the processing and then utf8encode it back and write.
0
 
LVL 6

Assisted Solution

by:_iskywalker_
_iskywalker_ earned 125 total points
ID: 21802948
like ciuly said. Take a form place a Tmemo and 2 buttons, doubleclick on the buttons so the procedure are registered (linked) in the dfm and use the following code (use a utf-8 file named bla.txt, i used some üöäß to verify since ascii (first 127 characters of the 256 possible) are coded normally fine):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   memo1.Lines.LoadFromFile('bla.txt');
   memo1.lines.Text:= utf8decode(memo1.lines.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   memo1.lines.Text:= utf8encode(memo1.lines.Text);
    memo1.Lines.SaveToFile('bla.txt');
end;

end.
0
 

Author Closing Comment

by:DelphiMan7
ID: 31467936
Thanks Ciuli and iskywalker, I've been trying to figure out how to do it all morning, it's not something I've needed to do before.  Thanks again!  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now