Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
how to create a text file with utf-8 encoding
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
accessing array members i...
by
TRon
[
Today
at 05:05:33 am]
How to DIY a filerec reco...
by
TRon
[
Today
at 03:59:17 am]
GTK3 still alpha
by
regs
[
Today
at 01:46:35 am]
2d "platform" game sugges...
by
flowCRANE
[
Today
at 12:25:28 am]
looping direction control...
by
440bx
[April 19, 2025, 08:08:37 pm]
ppaslink.sh: line 9: 6262...
by
Thaddy
[April 19, 2025, 07:58:29 pm]
JSON parser handler
by
cdbc
[April 19, 2025, 07:01:19 pm]
[SOLVED] Converting psd f...
by
TRon
[April 19, 2025, 06:58:36 pm]
This is me back in Lazaru...
by
TBMan
[April 19, 2025, 06:33:55 pm]
Avaiable instruction set
by
Thaddy
[April 19, 2025, 06:30:56 pm]
Best way to exchange data...
by
Hansvb
[April 19, 2025, 06:24:07 pm]
[Solved] How to call a cl...
by
nikel
[April 19, 2025, 05:09:22 pm]
Announcing TRURL RPN Engi...
by
jwdietrich
[April 19, 2025, 05:07:49 pm]
TRURL's segmitator availa...
by
jwdietrich
[April 19, 2025, 05:05:36 pm]
TRURL G released
by
jwdietrich
[April 19, 2025, 05:03:40 pm]
Animated Sine Flower
by
Thaddy
[April 19, 2025, 04:33:52 pm]
Compile Error on Assign f...
by
Thaddy
[April 19, 2025, 04:27:02 pm]
making types totally inco...
by
Thaddy
[April 19, 2025, 03:13:24 pm]
Why VirtualDBTreeEx not c...
by
wp
[April 19, 2025, 01:08:42 pm]
problem with easylazfreet...
by
ronhud
[April 19, 2025, 01:08:18 pm]
Forum slow for others as ...
by
Thaddy
[April 19, 2025, 11:25:43 am]
Errors linking the projec...
by
Thaddy
[April 19, 2025, 11:20:59 am]
in-memory machine code ge...
by
paule32
[April 19, 2025, 11:02:10 am]
Win11 - How to start cons...
by
dculp
[April 19, 2025, 12:21:51 am]
[SOLVED] Download link to...
by
Wings2018
[April 18, 2025, 10:37:08 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: how to create a text file with utf-8 encoding (Read 6113 times)
fxc
Newbie
Posts: 5
how to create a text file with utf-8 encoding
«
on:
February 11, 2016, 04:28:12 pm »
Hi
i tried with assignfile() and rewrite() , but the result file is asci
are there some compiler hints to switch to unicode api ?
thanks
Logged
Jonas Maebe
Hero Member
Posts: 1070
Re: how to create a text file with utf-8 encoding
«
Reply #1 on:
February 11, 2016, 04:38:48 pm »
The format in which data is written to a file is unrelated to any kind of API. It's all just bytes as far as the operating system is concerned.
If you have a text file, you can change the code page of the written data using the
http://www.freepascal.org/docs-html/rtl/system/settextcodepage.html
function. Passing CP_UTF8 will result in all data written to it to be converted to UTF-8.
Logged
fxc
Newbie
Posts: 5
Re: how to create a text file with utf-8 encoding
«
Reply #2 on:
February 11, 2016, 05:22:56 pm »
thanks Jonas , this function (SetTextCodePage) is what was missing , now the resulting file contain correct text
this is for text file , what if i want to write utf-8 text to a binary file ?
Logged
Jonas Maebe
Hero Member
Posts: 1070
Re: how to create a text file with utf-8 encoding
«
Reply #3 on:
February 11, 2016, 06:12:08 pm »
Quote from: fxc on February 11, 2016, 05:22:56 pm
this is for text file , what if i want to write utf-8 text to a binary file ?
A binary file is just arbitrary bytes. If the bytes you write represent UTF-8 text, then your file will contain UTF-8-encoded text.
Logged
fxc
Newbie
Posts: 5
Re: how to create a text file with utf-8 encoding
«
Reply #4 on:
February 11, 2016, 08:30:12 pm »
thanks again jonas , now i've a clear understanding
Logged
nk
Newbie
Posts: 2
Re: how to create a text file with utf-8 encoding
«
Reply #5 on:
March 14, 2025, 08:54:35 pm »
You could use
assignfile
or
TStringList
:
Code: Pascal
[Select]
[+]
[-]
program
project1
;
uses
Classes
,
SysUtils
;
var
F
:
Text
;
SL
:
TStringList
;
begin
// option 1
try
assignfile
(
f
,
'text1.txt'
,
CP_UTF8
)
;
Rewrite
(
f
)
;
writeln
(
f
,
'Привет мир'
)
;
writeln
(
f
,
'Möhren'
)
;
finally
Close
(
f
)
;
end
;
// option 2
SL
:
=
TStringList
.
Create
;
try
SL
.
Add
(
'∫cos(x)dx = sin(x) + C'
)
;
SL
.
Add
(
'¬(a ∧ b) ⇔ ¬a ∨ ¬b'
)
;
SL
.
Add
(
'Привет мир'
)
;
SL
.
Add
(
'Möhren'
)
;
SL
.
SaveToFile
(
'text2.txt'
,
TEncoding
.
UTF8
)
;
finally
SL
.
Free
;
end
;
end
.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
how to create a text file with utf-8 encoding
TinyPortal
© 2005-2018