得到“”“I”使用 delphi 从 .log 文件读取时作为输出数据

发布于 2024-12-08 17:53:16 字数 448 浏览 0 评论 0原文

我正在尝试从 .log 文件读取数据并处理其内容。日志文件是由另一个应用程序创建的。当我在Delphi中使用readln命令并在备忘录中显示文件内容时,我只从具有超过6000行数据的文件中获取一行数据(ÿþI)。

    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;

有谁知道可能是什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆萌少年 2024-12-15 17:53:16

正如 Michael 所说,您正在处理 UTF-16 编码的文件,因此您必须手动加载和解码它。网上有各种基于 WideString 的类似 TStringList 的类,或者 Borland 在 WideStrings 单元中有自己的实现,请尝试使用其中之一它们而不是 Pascal 文件 I/O,例如:

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; 

或者:

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; 

或者,安装 TNTWare 或 TMS 的副本,它们都具有支持 Unicode 的组件。然后您应该能够将 .log 文件直接 LoadFromFile() 放入您选择使用的 Unicode Memo 组件中。

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.

两仪 2024-12-15 17:53:16

您正在处理一个 UTF-16 文件(如前两个字符所示),而 Delphi 2007 没有为此做好准备,因此它会停止读取第一个 $0 字节,因为 Readln 认为线路到此结束。

您需要使用不同的方法来读取文件,并且必须读取 WideString(并且可能将其转换为字符串)。由于 Delphi 2007 不具备正确的 Unicode 功能,我想您还必须自己进行行分割,但我这里没有可用的功能,所以我不能完全确定。

You're dealing with a UTF-16 file (as evidenced by the first two characters), and Delphi 2007 is not prepared for that, so it stops reading on the first $0 byte, because Readln thinks the line ends there.

You'll need to use a different method of reading the file, and you'll have to read into a WideString (and probably convert that to a string). Since Delphi 2007 is not properly Unicode-capable, I think you'll also have to do your own line splitting, but I don't have that available here, so I'm not completely certain.

尹雨沫 2024-12-15 17:53:16

正如我在对 Remy 的回答的评论中提到的,在 WideStrings 中声明了一个 TWideStrings/TWideStringList:

uses WidesStrings;
//...
var
  Ws: TWideStrings;
  s: string;
  i: Integer;
begin
  Ws := TWideStringList.Create;
  try
    ws.LoadFromFile('C:\temp\UniTest.txt');
    for i := 0 to ws.Count - 1 do
    begin
      s := ws[i];
      Memo1.Lines.Add(s);
    end;
  finally
    ws.Free;
  end;
end;

但请注意,这不是 TStrings 后代,因此它不能直接分配给像 TMemo 这样的 TStrings 属性.线,你得一一添加。

它似乎也无法处理 BOM(您的 ÿþ)或大端编码

As mentioned in my comment to Remy's answer, there is a TWideStrings/TWideStringList declared in WideStrings:

uses WidesStrings;
//...
var
  Ws: TWideStrings;
  s: string;
  i: Integer;
begin
  Ws := TWideStringList.Create;
  try
    ws.LoadFromFile('C:\temp\UniTest.txt');
    for i := 0 to ws.Count - 1 do
    begin
      s := ws[i];
      Memo1.Lines.Add(s);
    end;
  finally
    ws.Free;
  end;
end;

Note however that is isn't a TStrings descendant, so it can't be directly assigned to TStrings properties like TMemo.Lines, you have to add them one by one.

It also doesn't seem to handle the BOM (your ÿþ) or big-endian encoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文