[fpc-pascal] Read lines into UnicodeString variable from UCS2 (UTF-16) encoded text file
Bart
bartjunk64 at gmail.com
Wed Sep 4 10:55:20 CEST 2019
Stupid an lazy workaround, probably not suitable for larger files.
{$mode objfpc}
{$h+}
uses
sysutils;
type
TUCS2TextFile = file of WideChar;
procedure ReadLine(var F: TUCS2TextFile; out S: UnicodeString);
var
WC: WideChar;
begin
//Assume file is opend for read
S := '';
while not Eof(F) do
begin
Read(F, WC);
if WC = WideChar(#$000A) then
exit
else
if (WC <> WideChar(#$000D)) and (WC<>WideChar(#$FEFF {Unicode LE
BOM})) then S := S + WC;
end;
end;
var
UFile: TUCS2TextFile;
US: UnicodeString;
begin
AssignFile(UFile, 'ucs2.txt');
Reset(Ufile);
while not Eof(UFile) do
begin
ReadLine(UFile, US);
writeln('US = ',US);
end;
CloseFile(UFile);
end.
Outputs
US = Line1
US = Line2
US = Line3
which is correct for my test file (Unicode LE encoding created with Notepad).
--
Bart
More information about the fpc-pascal
mailing list