Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unicode file

Posted on 2002-05-13
2
Medium Priority
?
342 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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