TFileStream类中怎么没有对文件进行按字节删除的方法?如何才能实现对Tfilestream创建的文件进行删除操作?(50分)

  • 主题发起人 主题发起人 深度历险
  • 开始时间 开始时间

深度历险

Unregistered / Unconfirmed
GUEST, unregistred user!
[:)]请各位不吝指教
 
function Seek(Offset: Longint
Origin: Word): Longint
overload
virtual;
function Seek(const Offset: Int64
Origin: TSeekOrigin): Int64
overload
virtual;

Description

Call Seek to move the current position of the stream in its particular storage medium (such as memory or a disk file).

The Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:

Value Meaning

soFromBeginning Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
soFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.
soFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.

Seek returns the new value of the Position property.

Seek is called by the Position and Size properties.

Note: As implemented in TStream, the two versions (the 32-bit or 64-bit syntax) call each other. Descendant stream classes must override at least one of these versions, and the override must not call the inherited default implementation.




Discards all data in the BLOB field from the current position on.
TBlobSteam
procedure Truncate;

Description

Use Truncate to limit the size of the BLOB data. Calling Truncate when the current position is 0 will clear the contents of the BLOB field.

Note: Do not call Truncate when the TBlobStream was created in bmRead mode.
 
不太明白
seek方法只能移动指针位置吧,要删除呢,tfilestream类中无truncate方法!
 
把起始指针的位置移动了然后在继续传送文件流不就可以把跳过的字符给删除了嘛
 
大概写了一个,将就着用吧.
//AStream 进行删除操作的 TStream 实例
//从 Stream 中 Position 为 Index 处开始删除 Count 个字节.注意 Index 从 0 开始.
procedure StreamDelete(AStream: TStream
Index: Longint
Count: Longint);
var
SavedPos: Longint;
ASize: Longint;
BufferStream: TMemoryStream;
begin
if not Assigned(AStream) then
exit;
ASize := AStream.Size;
if (Index >= ASize) or (Count = 0) then
exit;
if (Index + Count) >= ASize then
begin
AStream.Size := Index;
exit;
end
else begin
SavedPos := AStream.Position;
try
AStream.Seek(Index + Count, soFromBeginning);
BufferStream := TMemoryStream.Create;
try
ASize := ASize - (Index + Count);
BufferStream.SetSize(ASize);
BufferStream.Seek(0, soFromBeginning);
BufferStream.CopyFrom(AStream, ASize);
AStream.Size := Index + ASize;
AStream.Seek(Index, soFromBeginning);
AStream.Write(BufferStream.Memory^, ASize);
finally
BufferStream.Free;
end;
finally
AStream.Position := SavedPos;
end;
end;
end;
用法:
var
FileStream: TFileStream;
...
//对 TFileStream, OpenMode 必须为 fmOpenReadWrite,保证可读写.
FileStream := TFileStream.Create('c:/aaa.txt', fmOpenReadWrite);
StreamDelete(FileStream, 10, 5)
//从 Position 为 10(第 11 个字节)起删除 5 bytes.
...
 
[:)]我使用API函数:setendoffile解决了这个问题!!
 
后退
顶部