Solved

How do I work with UTF-8 formatted files?

Posted on 2008-06-17
3
4,792 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

615 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