Link to home
Start Free TrialLog in
Avatar of fish_r63
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;

Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

Check http://www.lischke-online.de/Unicode.html .

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
ASKER CERTIFIED SOLUTION
Avatar of Alone
Alone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial