hello,
i have a little question.
why crashes these find in file function when a file is writeprotected ?
i will not change datas i will oly search in it.
can someone give me the changes which i must do that the function will read writeprotected files ?
thansks...
Delphi-Quellcode:
function FindInFile(const FileName : string;
SearchWord : string; MatchCase : Boolean) : Integer;
var
fs : TFileStream;
Buffer : array [1..10000] of Char;
Size : Integer;
idx : Integer;
i : Integer;
begin
Result := -1;
idx := 1;
if not MatchCase then
SearchWord := UpperCase(SearchWord);
fs := TFileStream.Create(FileName, fmopenreadwrite or fmsharedenynone);
try
Repeat
Application.ProcessMessages;
Size := (Fs.Size - Fs.Position);
if Size > 10000 then Size := 10000;
Fs.ReadBuffer(Buffer, Size);
if not MatchCase then
begin
for i := 1 to Size do
Buffer[i] := Uppercase(Buffer)[i];
end;
for i := 1 to Size do
begin
if (Buffer[i] = SearchWord[idx]) then
Inc(idx)
else
idx := 1;
if (idx = Length(SearchWord)+1) then
begin
Result := (fs.Position - Size) + i - idx + 1;
Exit;
end;
end;
until fs.Position >= fs.Size;
finally
fs.Free;
end;
end;