Solved

How do I work with UTF-8 formatted files?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

830 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