Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何为WriteBuffer多次读取缓冲区_Delphi - Fatal编程技术网

Delphi 如何为WriteBuffer多次读取缓冲区

Delphi 如何为WriteBuffer多次读取缓冲区,delphi,Delphi,虽然bbuf和hbuff的值不同,但对于hdevi,没有发生任何变化 var bfile: TFileStream; hdevi: TFileStream; bbuff: array[0..511] of byte; hbuff: array[0..87039] of byte; curr: string; i: integer; begin curr:=GetCurrentDir; hdevi := TFileStr

虽然bbuf和hbuff的值不同,但对于
hdevi
,没有发生任何变化

var
  bfile:    TFileStream;
  hdevi:    TFileStream;
  bbuff:    array[0..511] of byte;
  hbuff:    array[0..87039] of byte;
  curr:     string;
  i:        integer;
begin
  curr:=GetCurrentDir;
  hdevi := TFileStream.Create(PChar(deviceno), fmOpenReadWrite);
  try
    bfile := TFileStream.Create(PChar(curr+'\bfile'), fmOpenReadWrite);
    try
      hdevi.ReadBuffer(hbuff[0],length(hbuff));
      bfile.ReadBuffer(bbuff[0],length(bbuff));
      hdevi.WriteBuffer(bbuff[0],length(bbuff));
      //for i:=0 to length(bbuff)-1 do
      //ShowMessage(IntToHex(hbuff[i],2)+'-'+IntToHex(bbuff[i],2));
    finally
      bfile.Free;
    end
  finally
    hdevi.Free;
  end;
end;
但在删除以下行后,它会工作

hdevi.ReadBuffer(hbuff[0],length(hbuff));
或添加此行
hdevi.Position:=0之前

hdevi.WriteBuffer(bbuff[0],length(bbuff));

我不知道为什么,有人能给我解释一下吗?

每次你调用
WriteBuffer
ReadBuffer
时,指向你当前所在位置的指针会向前移动
长度(…)
。因此,将位置移回0(查看
TStream
Seek
方法,请参阅文档)您的代码正在工作

每次调用
WriteBuffer
ReadBuffer
时,指向流中当前位置的指针向前移动
长度(…)
。因此,将位置移回0(查看
TStream
Seek
方法,请参阅文档)您的代码正在工作

我不知道为什么,有人能给我解释一下吗

每个
TStream
子代都有一个
Position
属性,该属性指示在流中的哪个字节位置将发生以下读或写操作。对于每个读取或写入操作,
位置
随着读取或写入的字节数而提前。要更改
位置
您可以分配到
位置
或直接调用
Seek()
函数(
Position
在内部使用
Seek()
函数)

让我们看一下这三行代码:

  // At this point both hdevi.Position and bfile.Position are 0
  hdevi.ReadBuffer(hbuff[0],length(hbuff));
  // At this point hdevi.Position is 87040
  bfile.ReadBuffer(bbuff[0],length(bbuff));
  // now bfile.Position is 512
  // hdevi.Position is of course still 87040, alas, that's where the bbuff is written
  hdevi.WriteBuffer(bbuff[0],length(bbuff));
  // hdevi.Position is now 87040 + 512 = 87552
显然,您希望将512字节从
bbuff
写入
hdevi
的开头。 如您所知,您可以跳过对
hbuff
的读取(在这种情况下,
hdevi.Position
为0),或者如果您需要先读取
hbuff
缓冲区,则必须将
hdevi.Position
重置为0

我不知道为什么,有人能给我解释一下吗

每个
TStream
子代都有一个
Position
属性,该属性指示在流中的哪个字节位置将发生以下读或写操作。对于每个读取或写入操作,
位置
随着读取或写入的字节数而提前。要更改
位置
您可以分配到
位置
或直接调用
Seek()
函数(
Position
在内部使用
Seek()
函数)

让我们看一下这三行代码:

  // At this point both hdevi.Position and bfile.Position are 0
  hdevi.ReadBuffer(hbuff[0],length(hbuff));
  // At this point hdevi.Position is 87040
  bfile.ReadBuffer(bbuff[0],length(bbuff));
  // now bfile.Position is 512
  // hdevi.Position is of course still 87040, alas, that's where the bbuff is written
  hdevi.WriteBuffer(bbuff[0],length(bbuff));
  // hdevi.Position is now 87040 + 512 = 87552
显然,您希望将512字节从
bbuff
写入
hdevi
的开头。
如您所知,您可以跳过对
hbuff
的读取(在这种情况下,
hdevi.Position
为0),或者如果您需要先读取
hbuff
缓冲区,则必须将
hdevi.Position
重置为0。

读取更改流的位置。读取更改流的位置。