Delphi 如何为WriteBuffer多次读取缓冲区
虽然bbuf和hbuff的值不同,但对于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
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。读取更改流的位置。读取更改流的位置。