Solved

How do I work with UTF-8 formatted files?

Posted on 2008-06-17
3
4,636 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:
2266180 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

809 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