Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading unicode text file in delphi 2007 !

Posted on 2011-10-25
6
Medium Priority
?
2,685 Views
Last Modified: 2012-05-12
Hello,

i need to read a unicode text file and store it in string.

i failed to do that using LoadFromFile function already exists in delphi.

would u help me please ?
0
Comment
Question by:hintco
5 Comments
 
LVL 1

Author Comment

by:hintco
ID: 37023585
or i need a function converts unicode text file into ansi text file.
0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 1000 total points
ID: 37029115
here, you can find how to have some degree of Unicode-capability in applications made with Delphi pre 2009

http://stackoverflow.com/questions/383195/handling-a-unicode-string-in-delphi-versions-2007
0
 
LVL 5

Accepted Solution

by:
MvanderKooij earned 1000 total points
ID: 37029636
I'm using the following code to read and write Unicode files with Delphi 2007. When I compile the same project in Delphi XE2 I do not use this but the unicode encoding. This way I can handle text files created with both ansi and unicode version of the same project.
(I use ifdefs to decide to use the unicode encoding for XE2 or those function for Delphi 2007 ansi)
 
function LoadAsUtf8(strFileName: AnsiString): AnsiString;
var
  Stream: TStream;
  bomHeader: array[0..2] of byte;

  strFileContents: UTF8String;
begin
  Stream := TFileStream.Create(strFileName, fmOpenRead);
  try
    Stream.Read(bomHeader, 3);
    if (bomHeader[0] = $EF) and (bomHeader[1] = $BB) and (bomHeader[2] = $BF) then begin
      SetLength(strFileContents, Stream.Size -3);
      Stream.ReadBuffer(Pointer(strFileContents)^, Stream.Size -3);
    end else begin
      Stream.Position := 0;
      SetLength(strFileContents, Stream.Size);
      Stream.ReadBuffer(Pointer(strFileContents)^, Stream.Size);
    end;
  finally
    Stream.Free;
  end;
  Result := Utf8ToAnsi(strFileContents);
end;

procedure SaveAsUtf8(strFileName, strText: AnsiString);
var
  Stream: TStream;
  bomHeader: array[0..2] of byte;
begin
  strText := AnsiToUtf8(strText);
  Stream := TFileStream.Create(strFileName, fmCreate);
  try
    bomHeader[0] := $EF;
    bomHeader[1] := $BB;
    bomHeader[2] := $BF;
    Stream.Write(bomHeader, 3);
    Stream.WriteBuffer(Pointer(strText)^, Length(strText));
  finally
    Stream.Free;
  end;
end;

Open in new window

0
 
LVL 1

Author Comment

by:hintco
ID: 37049626
MvanderKooij:

thank you for the function but i need the same thing for UTF16
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 37205967
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

580 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