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
Range check, converting e...
by
jamie
[
Today
at 12:06:42 pm]
Practical Ways to Help La...
by
Joanna from IRC
[
Today
at 12:05:57 pm]
TSynCompletion failing wh...
by
Ñuño_Martínez
[
Today
at 11:38:11 am]
Animated Oscillating Line...
by
Dzandaa
[
Today
at 11:31:11 am]
TBCFluentProgressRing bug
by
circular
[
Today
at 11:27:35 am]
Pdf file size is huge wit...
by
meeeeeeeeee
[
Today
at 10:35:12 am]
Why isn't Lazarus / Free ...
by
alpine
[
Today
at 10:20:45 am]
BGRAGtkBitmap
by
circular
[
Today
at 09:52:07 am]
FPC 3.2.2 + NASM Windows ...
by
munair
[
Today
at 09:23:47 am]
Anyone into mDNS?
by
MaxM74
[
Today
at 08:27:51 am]
Animated toggle switch
by
hedgehog
[
Today
at 07:37:00 am]
I can write your README.m...
by
GAN
[
Today
at 03:51:30 am]
3D Dot Ball
by
Gigatron
[
Today
at 12:37:46 am]
A little bug in fpsimages...
by
wp
[May 15, 2025, 11:21:50 pm]
Feature request/suggestio...
by
440bx
[May 15, 2025, 10:37:14 pm]
Lazarus Release 4.0
by
Hansvb
[May 15, 2025, 10:20:16 pm]
A problem with ptcmouse
by
TBMan
[May 15, 2025, 09:40:51 pm]
Why there is no build-in ...
by
Nimbus
[May 15, 2025, 08:50:15 pm]
Feature request/suggestio...
by
440bx
[May 15, 2025, 07:58:33 pm]
Build mode Help button do...
by
440bx
[May 15, 2025, 07:54:27 pm]
How to convert TStrings t...
by
Remy Lebeau
[May 15, 2025, 06:35:35 pm]
[SOLVED] Added components...
by
Rico54
[May 15, 2025, 03:27:20 pm]
[SOLVED] How to "Jump To"...
by
paweld
[May 15, 2025, 02:40:52 pm]
PZ_Nes emulator
by
Ñuño_Martínez
[May 15, 2025, 02:29:40 pm]
Problems with Word
by
loaded
[May 15, 2025, 02:17:56 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: how to create a text file with utf-8 encoding (Read 6149 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