Notice Of Closure

Please note that this website is due to close down on 15th June 2020.

In the interim I will try to move as much content as possible over to my GitHub account. Thanks to everyone who has supported the site over the years.

» Help

How to read Unicode text files

I'd like to read text from a unicode text file, but don't know how to do this. It looks like ReadLn only works with single-byte character sets.

Not tested:

function ReadUnicodeTextFile(aFName: string): WideString;
  buf: array[0..1024] of WideChar;
  f: file;
  dwRest, dwRead: DWORD;
  result := '';
  Assign(f, aFName);
  Reset(f, 1);
  dwRest := FileSize(f);
  Assert((dwRest mod SizeOf(widechar)) = 0); {Unicode chars always 2 bytes}
  while dwRest > 1do
    BlockRead(f, buf, sizeof(buf), dwRead);
    if dwRest > dwRead then
      Dec(dwRest, dwRead)
      dwRest := 0;
    buf[dwRead mod 2] := WideChar(#0);
    result := result + PWideString(buf);
Original resource: The Delphi Pool
Author: Jens Geyer
Added: 2010-06-02
Last updated: 2010-06-02

« Return to contents »