你如何将 Delphi 中的文件读入字节数组?

问题描述

我正在尝试将文件读入 Delphi XE2 中的字节数组。

这是我当前的代码

function FiletoBytes(const AName: string; var Bytes: TBytes): Boolean;
var
  Ms: TMemoryStream;
begin
  Result := False;
  if not FileExists(AName) then
    Exit;
  Ms := TMemoryStream.Create;
  try
    Ms.LoadFromFile(AName);
    if Ms.Size > 0 then
    begin
      Ms.Position := 0;
      MS.ReadBuffer(Bytes[0],Ms.Size);
      Result := True;
    end;
  finally
    Ms.Free;
  end;
end;

procedure runFile();
var
  Bytes: TBytes;
  OpFile: String;
begin
  OpFile := 'C:\Users\Kenny\Documents\calc.exe';
  Bytes := nil;
  if FiletoBytes(OpFile,Bytes) then
  begin
    //do someting with Bytes(array of Byte)

  end;
end;

在这一行遇到错误

MS.ReadBuffer(Bytes[0],Ms.Size);

错误是:

0x00404727 处的访问冲突:写入地址 0x00000008

解决此问题的任何帮助将不胜感激。

解决方法

您没有分配数组,这解释了错误。您可以像这样修复您的代码:

Ms.LoadFromFile(AName);
SetLength(Bytes,Ms.Size);
Ms.Position := 0;
MS.ReadBuffer(Pointer(Bytes)^,Ms.Size);
Result := True;

请注意,我避免了检查零长度文件的需要,并使用了 Pointer(Bytes) 以便代码在范围检查处于活动状态时可以工作。

我还注意到您的代码违反了我所说的 Delphi 内存流反模式。您将文件读入内存流,它本质上是一个字节数组。然后你从那个字节数组复制到另一个字节数组。您将整个文件写入两个单独的字节数组。这是一个不必要的。最好这样写:

function FileToBytes(const AName: string; var Bytes: TBytes): Boolean;
var
  Stream: TFileStream;
begin
  if not FileExists(AName) then
  begin
    Result := False;
    Exit;
  end;
  Stream := TFileStream.Create(AName,fmOpenRead);
  try
    SetLength(Bytes,Stream.Size);
    Stream.ReadBuffer(Pointer(Bytes)^,Stream.Size);
  finally
    Stream.Free;
  end;
  Result := True;
end;

这样你就可以直接从文件中读取到目标字节数组中。

我不太喜欢把它作为一个返回布尔值来表示成功的函数。除了文件不存在之外,还有很多方法会导致代码失败。这些将导致您的设计出现异常。我更愿意看到纯基于异常的错误处理,或纯基于错误代码的错误处理。但不是您设计的混合方法。

正如 Andreas 在评论中指出的那样,您还可以使用 RTL 库函数 System.IOUtils.TFile.ReadAllBytes 来执行此任务。虽然我个人倾向于避免使用整个 System.IOUtils 单元,因为有许多设计问题。虽然其中一些已在最近的版本中得到整理,但我认为 XE2 版本可能会出现一些问题。