从二进制文件中删除char

Rem*_*igo 3 delphi

我想从二进制文件/文本文件中删除一个字符/字符串.如果我从文件中知道char/string的位置,我该如何删除它?我应该读取文件(BlockRead),删除char/string(使用Delete(source,startPos,endPos)然后将(BlockWrite)写入新文件,或者我可以直接从指定文件中删除?

谢谢

Ala*_*ark 12

您可以使用TFileStream.CopyFrom方法复制到不需要的字符串之前,先搜索它,然后再复制CopyFrom文件的其余部分.TFileStreams非常快.

像这样的东西(未经测试)

aInFile := TFileStream.Create(sInput, fmOpenRead);
try
  aOutFile := TFileStream.Create(sOutput, fmCreate);
  try
    aOutFile.CopyFrom(aInFile, Pos);
    aInFile.Seek(Skip);
    aOutFile.CopyFrom(aInFile, aInfile.Size - Pos - Skip);
  finally
    aOutFile.Free;
  end;
finally
  aInFile.Free;
end;
Run Code Online (Sandbox Code Playgroud)