?
Solved

Unicode file

Posted on 2002-05-13
2
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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