Search code examples
delphidelphi-7delphi-2007

Getting "ÿþI" as output data when reading from a .log file using delphi


I am trying to read data from a .log file and process its contents. The log file is created by another application. When I use the readln command in Delphi and display the contents of the file in a memo, I only get the one line of data (ÿþI) from a file with over 6000 lines of data.

    procedure TForm1.Button1Click(Sender: TObject);
    Var
        F : TextFile;
        s : string;
    begin
        AssignFile(F, 'data.log');
        Reset(F);

        while not Eof(F) do
        begin
            Readln(F, s);
            Memo1.Lines.Add(s);
        end;
    end;

Does anyone know what the problem might be?


Solution

  • As Michael said, you are dealing with a UTF-16 encoded file, so you will have to load and decode it manually. There are various WideString-based TStringList-like classes floating around online, or Borland has its own implementation in the WideStrings unit, try using one of them instead of Pascal file I/O, eg:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      SL : TWideStringList;
      I: Integer;
      s : string;
    begin
      SL := TWideStringList.Create;
      try
        SL.LoadFromFile('data.log');
        Memo1.Lines.BeginUpdate;
        try
          for I := 0 to SL.Count-1 do
            Memo1.Lines.Add(SL[I]);
        finally
          Memo1.Lines.EndUpdate;
        end;
      finally
        SL.Free;
      end;
    end; 
    

    Or:

    uses
      .., WideStrings;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      SL : TWideStringList;
    begin
      SL := TWideStringList.Create;
      try
        SL.LoadFromFile('data.log');
        Memo1.Lines.Assign(SL);
      finally
        SL.Free;
      end;
    end; 
    

    Alternatively, install a copy of TNTWare or TMS, which both have Unicode-enabled components. Then you should be able to just LoadFromFile() the .log file directly into whicher Unicode Memo component you chose to use.