【问题标题】:Isn't the size of Delphi TFileStream WriteBuffer Int64? I'm only able to write small amounts at a timeDelphi TFileStream WriteBuffer Int64的大小不是吗?我一次只能写少量
【发布时间】:2019-02-26 03:01:02
【问题描述】:

我正在尝试编写更大的块以提高文件保存的速度。我有大约 9 个循环要转换,但我不知道我做错了什么

fs := TFileStream.Create(Myfile, fmCreate);

此代码有效:

for RecordGroup := 0 to TotalGroups - 1 do
begin
  for RecordNumber := 0 to Length(MyArray[RecordGroup]) - 1 do
  begin
    fs.WriteBuffer(MyArray[RecordGroup,RecordNumber],SizeOf(MyRecord));
  end;
end;

当我删除内循环以编写更大的块时,代码不起作用:

for RecordGroup := 0 to TotalGroups - 1 do
begin
  fs.WriteBuffer(MyArray[RecordGroup],SizeOf(MyRecord) * Length(MyArray[RecordGroup]));
end;

我收到一般错误“流写入错误”

SizeOf(MyRecord) * Length(MyArray[RecordGroup]) 的值为 180 * 152,004 = 27,360,720

我读过的所有内容基本上都说这是正确的。任何想法我做错了什么?

提前感谢您提出的任何想法。

【问题讨论】:

  • 实际上它只会让你达到2gb阅读我的Q stackoverflow.com/questions/48039254
  • LU RD,我忘了减1。我编辑更正了。这就是你说的显眼吗?
  • 是的,如果没有它,代码将无法正常工作。

标签: delphi tfilestream


【解决方案1】:

将编写代码更改为(注意方括号中的附加0)

fs.WriteBuffer(MyArray[RecordGroup, 0],   SizeOf(MyRecord) * Length(MyArray[RecordGroup]));

错误在于错误的动态数组使用。我假设MyArray 是二维数组,所以MyArray[RecordGroup] 是一维动态数组——本质上是指向数据的指针——但是WriteBuffer 的无类型变量参数需要使用数组数据体。


旁注 - 您的 for 循环计数器从 0 变为 TotalGroups,因此循环总数为 TotalGroups + 1。对吗?

【讨论】:

  • 谢谢 MBo!我非常关注 Count 参数,我没有想到它可能是数组。我也忘记从循环中减去 1。我进行了编辑以解决该问题。
猜你喜欢
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
相关资源
最近更新 更多