帮忙看一下为何写在流中的字符串找不到(40分)

  • 主题发起人 主题发起人 dafuwengqing
  • 开始时间 开始时间
D

dafuwengqing

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾
帮忙看一下为何写在流中的字符串找不到?
调用如下:
SetHTMLInf('c:/a.html','HTML1');
fileInf:=GetHTMLInf('c:/a.html','HTML')
调试时ScanStream 老是返回-1 没有找到!
函数如下:
procedure TForm1.SetHTMLInf(FileName,Inf:string);
var
HTMLStream:TMemoryStream;
begin
HTMLStream:=TMemoryStream.Create;
HTMLStream.loadfromfile(FileName);
HTMLStream.Seek(0,soFromEnd);
HTMLStream.Writebuffer(Inf,length(Inf));
HTMLStream.SaveToFile(FileName);
HTMLStream.Free;
end;

function TForm1.GetHTMLInf(FileName,InfFlag:string):string;
var
HTMLStream:TMemoryStream;
myresult:string;
InfPos:integer;
myBuffer:string;
begin
HTMLStream:=TMemoryStream.Create;
HTMLStream.loadfromfile(FileName);
//获取InfoFlag在流中的位置
InfPos:=ScanStream(HTMLStream,InfFlag);
//如果找不到
if InfPos< 0 then
begin
result:='';
end;
HTMLStream.Seek(InfPos+length(InfFlag),soFromBeginning);
HTMLStream.ReadBuffer(myBuffer,HTMLStream.Size-InfPos-length(InfFlag));
myresult:=myBuffer;
HTMLStream.Free;
result:=myresult;
end;

function TForm1.ScanStream(mStream: TStream; mStr: string): Integer;
const
cBufferSize = $8000;
var
S: string;
T: string;
I: Integer;
L: Integer;
begin
Result := -1;
if not Assigned(mStream) then Exit;
if mStr = '' then Exit;
L := Length(mStr);
mStream.Position := 0;
SetLength(S, cBufferSize);
T := '';
for I := 1 to mStream.Size div cBufferSize do begin
mStream.Read(S[1], cBufferSize);
Result := Pos(mStr, T + S) - 1; //保留上次搜索的尾部字符~~
T := Copy(S, cBufferSize - L, MaxInt);
if Result >= 0 then begin
Result := Result + Pred(I) * cBufferSize - Length(T);
Exit;
end;
end;
I := mStream.Size mod cBufferSize;
SetLength(S, I);
if I > 0 then begin
mStream.Read(S[1], I);
Result := Pos(mStr, T + S) - 1;
if Result >= 0 then begin
Result := Result + mStream.Size - I - Length(T);
Exit;
end;
end;
end;
 
procedure TForm1.SetHTMLInf(FileName,Inf:string);
函数在实现的时候,应该定位:
HTMLStream.Seek(0,soFromBeginning);
或者干脆:HTMLStream.Positon := 0;
 
老大
我有Seek呀 HTMLStream.Seek(0,soFromEnd);
我是先定位到文件末再添加信息!
 
HTMLStream.ReadBuffer(myBuffer,HTMLStream.Size-InfPos-length(InfFlag));
计算错误?
 

Similar threads

I
回复
0
查看
660
import
I
I
回复
0
查看
644
import
I
I
回复
0
查看
685
import
I
I
回复
0
查看
591
import
I
I
回复
0
查看
585
import
I
后退
顶部