Solved

Unicode file

Posted on 2002-05-13
2
329 Views
Last Modified: 2010-04-04
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;

0
Comment
Question by:fish_r63
2 Comments
 
LVL 8

Expert Comment

by:Cesario
ID: 7007033
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
0
 
LVL 1

Accepted Solution

by:
Alone earned 50 total points
ID: 7007052
Please, ty this sample:

var
  F: TStream;
  UnicodeString: WideString;
  UnicodeSign: Word;
  FileName: string;
  FileSize: Cardinal;
begin
  F := TFileStream.Create(FileName, fmOpenRead);
  try
    FileSize := F.Size;
    if FileSize >= SizeOf(UnicodeSign) then
    begin
      F.ReadBuffer(UnicodeSign, SizeOf(UnicodeSign));
      if UnicodeSign = $FEFF then
      begin
        Dec(FileSize, SizeOf(UnicodeSign);
        SetLength(UnicodeString, FileSize div SizeOf(WideChar));
        F.ReadBuffer(UnicodeString[1], FileSize);
        // now UnicodeString contains Unicode string read from stream
      end
      else
        // not a Unicode format;
    end;
  finally
    F.Free;
  end;
end;

WBR...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now