Solved

Unicode file

Posted on 2002-05-13
2
328 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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

13 Experts available now in Live!

Get 1:1 Help Now