Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I work with UTF-8 formatted files?

Posted on 2008-06-17
3
Medium Priority
?
4,969 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 500 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 500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

877 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