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
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
[SOLVED] The "dockedforme...
by
jamie
[
Today
at 10:50:42 am]
Set horizontal scroll pos...
by
jamie
[
Today
at 10:40:58 am]
Amigo programming languag...
by
srvaldez
[
Today
at 10:29:37 am]
Lazarus Bugfix Release 4....
by
Ameta
[
Today
at 10:28:14 am]
Win 11, strange position ...
by
d7_2_laz
[
Today
at 10:24:29 am]
How many lines is too man...
by
440bx
[
Today
at 09:46:20 am]
Debian removes FPC/Lazaru...
by
MarkMLl
[
Today
at 08:45:47 am]
Qt6/Wayland clipboard: pa...
by
AlexTP
[
Today
at 08:15:04 am]
The Case For Pascal, 55 Y...
by
valdir.marcos
[
Today
at 08:02:58 am]
Text Fill (Memo)
by
paweld
[
Today
at 07:14:04 am]
Update a table with an Au...
by
Zvoni
[
Today
at 07:12:12 am]
Front-end framework
by
PierceNg
[
Today
at 05:46:34 am]
X11Libre, finally and for...
by
Fred vS
[
Today
at 03:37:59 am]
https://live.freepascal.o...
by
PierceNg
[
Today
at 03:04:22 am]
PasLLM - LLM Inference En...
by
valdir.marcos
[
Today
at 01:11:25 am]
Fast Canvas Library V1.05...
by
Gigatron
[
Today
at 01:09:53 am]
TDirectoryEdit with OnAft...
by
Bart
[
Today
at 01:04:32 am]
Problem wih reference to ...
by
PascalDragon
[
Today
at 12:09:44 am]
Benchmark test in nanosec...
by
PascalDragon
[March 07, 2026, 11:14:15 pm]
How to execute a procedur...
by
n7800
[March 07, 2026, 10:17:32 pm]
[SOLVED] Add Help to an A...
by
valdir.marcos
[March 07, 2026, 07:18:00 pm]
Variable initialization
by
valdir.marcos
[March 07, 2026, 05:58:47 pm]
Lazarus IDE built for LCL...
by
valdir.marcos
[March 07, 2026, 05:57:09 pm]
Status of LCL-fpGUI widge...
by
valdir.marcos
[March 07, 2026, 04:04:53 pm]
Status of LCL's CustomDra...
by
zeljko
[March 07, 2026, 03:59:56 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: how to create a text file with utf-8 encoding (Read 6963 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: 1071
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: 1071
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