fish_r63
asked on
Unicode file
I need to read a file in Unicode format. How can i do this.
If i do something like this, it does not work.
var F: TextFile;
S, SQLText: widestring;
begin
AssignFile(F, FileName);
Reset(F);
while not EOF(F) do
begin
Readln(F, S);
end;
If i do something like this, it does not work.
var F: TextFile;
S, SQLText: widestring;
begin
AssignFile(F, FileName);
Reset(F);
while not EOF(F) do
begin
Readln(F, S);
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can probably read this file by a routine like this, which converts it to
ANSI.
Procedure LoadUnicodeFile( const filename: String; strings: TStrings );
Procedure SwapWideChars( p: PWideChar );
Begin
While p^ <> #0000 Do Begin
p^ := WideChar(Swap( Word(p^)));
Inc( p );
End; { While }
End; { SwapWideChars }
Var
ms: TMemoryStream;
wc: WideChar;
pWc: PWideChar;
Begin
ms:= TMemoryStream.Create;
try
ms.LoadFromFile( filename );
ms.Seek( 0, soFromend );
wc := #0000;
ms.Write( wc, sizeof(wc));
pWC := ms.Memory;
If pWc^ = #$FEFF Then // normal byte order mark
Inc(pWc)
Else If pWc^ = #$FFFE Then Begin // byte order is big-endian
SwapWideChars( pWc );
Inc( pWc );
End { If }
Else ; // no byte order mark
strings.Text := WideCharToString( pWc );
finally
ms.free;
end;
End; { LoadUnicodeFile }
Use this like
LoadUnicodeFile( filename, memo1.lines );
Untested!
Best Regards
Cesario