delphi 删除任意文件的任意位置的任意数量的字符


本文整理自网络,侵删。

  function DelChar(AFile: string; DelStart, DelCount: integer): integer; 

//如果作为窗体的过程,过程名前要加窗体的类名,比如 TForm1.DelChar
//AFile 是欲处理的文件,DelStart 是欲删除字符的开始位置,DelCount 是欲删除的字符数
//注意,DelStart的基数为0
//删除成功,函数返回0,不成功则返回1
//转自 hiya_data 在 CSDN 的回答
var
s1, s2: string;
f1, f2: tFileStream;
c1: PChar;
iRead, iReaded: integer;
k: integer;
SearchRec1: tSearchRec;
begin
Result := 0;
if DelStart < then exit;

try

f1 := tFileStream.Create(AFile, fmOpenRead or fmShareExclusive);
try
if DelStart > (f1.Size-1) then exit;
s2 := ExtractFileName(AFile);
s1 := ExtractFileDir(AFile);
k := 1;
s1 := s1 + 'Bak' + IntToStr(k) + '_' + s2;
while FindFirst(s1, faAnyFile, SearchRec1) = 0 do begin
Inc(k);
s1 := s1 + 'Bak' + IntToStr(k) + '_' + s2;
end;
f2 := tFileStream.Create(s1, fmCreate or fmShareExclusive);
try
f1.Position := 0;
f2.Position := 0;
GetMem(c1, 1024);
iRead := Min(1024, DelStart-f1.Position);
while iRead > 0 do begin
f1.Read(c1^, iRead);
f2.Write(c1^, iRead);
iRead := Min(1024, DelStart-f1.Position);
end;
f1.Position := DelStart + DelCount;
repeat
iReaded := f1.Read(c1^, 1024);
f2.Write(c1^, iReaded);
until (iReaded < 1024) or (f1.Position = f1.Size);
FreeMem(c1);
finally
f2.Free;
end;
finally
f1.Free;
end;
DeleteFile(AFile);
RenameFile(s1, AFile);

except
Result := 1;
end;

end;

相关阅读 >>

Delphi 查找一个特定的exe是否在内存中运行

Delphi 结束360safe和360保险箱进程

Delphi 使用google translate实现tts

Delphi http post json示例

Delphi 编写activex控件(ocx控件)的知识和样例

Delphi xe5 android 调用手机震动

Delphi httpencode

Delphi checkbox 透明

Delphi实现获取密码框中的密码

Delphi 标题栏相关操作

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...