V
vivee
Unregistered / Unconfirmed
GUEST, unregistred user!
我找到一个函数,功能是找到文件中“指定字符串”的位置。
function ScanFile(const FileName: string;
const forString: string;
caseSensitive: Boolean): Longint;
const
BufferSize = $8001; { 32K+1 bytes }
var
pBuf, pEnd, pScan, pPos: PChar;
filesize: LongInt;
bytesRemaining: LongInt;
bytesToRead: Integer;
F: file;
SearchFor: PChar;
oldMode: Word;
begin
{假设失败}
Result := -1;
if (Length(forString) = 0) or (Length(FileName) = 0) then Exit;
SearchFor := nil;
pBuf := nil;
{使用二进制方式打开文件, 1 字节记录 }
AssignFile(F, FileName);
oldMode := FileMode;
FileMode := 0; {只读访问 }
Reset(F, 1);
FileMode := oldMode;
try {分配内存缓冲}
SearchFor := StrAlloc(Length(forString) + 1);
StrPCopy(SearchFor, forString);
if not caseSensitive then {转换成大写}
AnsiUpper(SearchFor);
GetMem(pBuf, BufferSize);
filesize := System.Filesize(F);
bytesRemaining := filesize;
pPos := nil;
while bytesRemaining > 0 do
begin
if bytesRemaining >= BufferSize then
bytesToRead := Pred(BufferSize)
else
bytesToRead := bytesRemaining;
{ read a buffer full and zero-terminate the buffer }
BlockRead(F, pBuf^, bytesToRead, bytesToRead);
pEnd := @pBuf[bytesToRead];
pEnd^ := #0;
pScan := pBuf;
while pScan < pEnd do
begin
if not caseSensitive then {转换为大写}
AnsiUpper(pScan);
pPos := StrPos(pScan, SearchFor); {搜索子串 }
if pPos <> nil then
begin { Found it! }
Result := FileSize - bytesRemaining +
Longint(pPos) - Longint(pBuf);
Break;
end;
pScan := StrEnd(pScan);
Inc(pScan);
end;
if pPos <> nil then Break;
bytesRemaining := bytesRemaining - bytesToRead;
if bytesRemaining > 0 then
begin
Seek(F, FilePos(F) - Length(forString));
bytesRemaining := bytesRemaining + Length(forString);
end;
end;
finally
CloseFile(F);
if SearchFor <> nil then StrDispose(SearchFor);
if pBuf <> nil then FreeMem(pBuf, BufferSize);
end;
end;
我想改动这个函数,使它能够从文件中搜索“特征字符串”(比如email地址)的内容。
谁能帮我完成,成功后我再奉送300!!
function ScanFile(const FileName: string;
const forString: string;
caseSensitive: Boolean): Longint;
const
BufferSize = $8001; { 32K+1 bytes }
var
pBuf, pEnd, pScan, pPos: PChar;
filesize: LongInt;
bytesRemaining: LongInt;
bytesToRead: Integer;
F: file;
SearchFor: PChar;
oldMode: Word;
begin
{假设失败}
Result := -1;
if (Length(forString) = 0) or (Length(FileName) = 0) then Exit;
SearchFor := nil;
pBuf := nil;
{使用二进制方式打开文件, 1 字节记录 }
AssignFile(F, FileName);
oldMode := FileMode;
FileMode := 0; {只读访问 }
Reset(F, 1);
FileMode := oldMode;
try {分配内存缓冲}
SearchFor := StrAlloc(Length(forString) + 1);
StrPCopy(SearchFor, forString);
if not caseSensitive then {转换成大写}
AnsiUpper(SearchFor);
GetMem(pBuf, BufferSize);
filesize := System.Filesize(F);
bytesRemaining := filesize;
pPos := nil;
while bytesRemaining > 0 do
begin
if bytesRemaining >= BufferSize then
bytesToRead := Pred(BufferSize)
else
bytesToRead := bytesRemaining;
{ read a buffer full and zero-terminate the buffer }
BlockRead(F, pBuf^, bytesToRead, bytesToRead);
pEnd := @pBuf[bytesToRead];
pEnd^ := #0;
pScan := pBuf;
while pScan < pEnd do
begin
if not caseSensitive then {转换为大写}
AnsiUpper(pScan);
pPos := StrPos(pScan, SearchFor); {搜索子串 }
if pPos <> nil then
begin { Found it! }
Result := FileSize - bytesRemaining +
Longint(pPos) - Longint(pBuf);
Break;
end;
pScan := StrEnd(pScan);
Inc(pScan);
end;
if pPos <> nil then Break;
bytesRemaining := bytesRemaining - bytesToRead;
if bytesRemaining > 0 then
begin
Seek(F, FilePos(F) - Length(forString));
bytesRemaining := bytesRemaining + Length(forString);
end;
end;
finally
CloseFile(F);
if SearchFor <> nil then StrDispose(SearchFor);
if pBuf <> nil then FreeMem(pBuf, BufferSize);
end;
end;
我想改动这个函数,使它能够从文件中搜索“特征字符串”(比如email地址)的内容。
谁能帮我完成,成功后我再奉送300!!