Bonjour,
Quelqu'un sait-il comment on peut rechercher l'existence d'un mot dans un fichier, quel que soit sont type (binaire ou non) ?
Bonjour,
Quelqu'un sait-il comment on peut rechercher l'existence d'un mot dans un fichier, quel que soit sont type (binaire ou non) ?
Ca peut ressembler à ceci
Si ce sont de gros fichiers...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 //============================================================================== //=== Fonction pour rechercher une string dans un fichier binaire ou texte //=== La fonction charge completement le fichier en memoire dans un TMemoryStream //=== avant d'effectuer la recherche. function FindStrInFile(Filename:string;StrToFind:string):boolean; var BigBuf:TMemoryStream; i:LongInt; LengthOfStr:LongInt; begin FindStrInFile:=FALSE; LengthOfStr:=Length(StrToFind); BigBuf:=TMemoryStream.Create; try BigBuf.LoadFromFile(FileName); for i:=0 to (BigBuf.Size-LengthOfStr) do begin if CompareMem(@PChar(BigBuf.Memory)[i], PChar(StrToFind), LengthOfStr) then begin FindStrInFile:=TRUE; exit; end; end; finally BigBuf.Free; end; end;
faudrait alors revoir la stratégie...
C'est bien ce que tu cherchais?
Comment dupliquer un disque...ça vous intéresse?
Tutoriel et code source delphi ici
Merci mais j'ai avancé depuis le 17/11.
J'utilise un TStream de la façon suivante
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 Function FindWordInFile(FileName : TFileName; StringToFind: String; CaseSensitive: Boolean = false; WholeWord : Boolean = False; StartAt: Integer = 1): Integer; Var Offset, EndStrToFind, StrLen: Integer; FileStr, StrToFind: string; FStream: TFileStream; i : Integer; begin Result := 0; FStream := TFileStream.Create(FileName, fmOpenRead); try SetLength(FileStr, FStream.Size); FStream.Read(FileStr[1], FStream.Size); finally FStream.Free; end; StrLen := Length(StringToFind); If Length(FileStr) = 0 Then Exit; If StrLen = 0 Then begin //un string de longueur nulle result := 1; //sera toujours trouvé Exit; end; If not CaseSensitive Then begin StringToFind:= AnsiLowerCase(StringToFind); FileStr := AnsiLowerCase(FileStr); end; EndStrToFind := StartAt + StrLen; for Offset := StartAt to Length(FileStr)- StrLen +1 do begin if StringToFind[1] = FileStr[Offset] then begin if (StrLComp(@FileStr[Offset], @StringToFind[1], StrLen) = 0) then begin if ((WholeWord = True) and ((Offset = 1) or not IsCharAlphaNumeric(FileStr[Offset-1])) and ((EndStrToFind > Length(FileStr)) or not IsCharAlphaNumeric(FileStr[EndStrToFind])) or WholeWord = False) then begin Result := Offset; Exit; end; end; end; Inc(EndStrToFind); end; End;
Merci pour ta fonction, cependant, je l'ai testé avec un gros fichier
environ 488Meg... et j'ai eu un petit problème avec ceci:
je l'ai remplacé par:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 If not CaseSensitive Then begin StringToFind:= AnsiLowerCase(StringToFind); FileStr := AnsiLowerCase(FileStr); //--->Manque de memoire ici! end;
AnsiLowerCase créer une copie de la string initiale tandis que
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 If not CaseSensitive Then begin StringToFind:= AnsiLowerCase(StringToFind); CharLowerBuff(PChar(FileStr),Length(FileStr)); end;
CharLowerBuff fait le même travaille directement dans la string.
Ca suppose aussi que c'est plus rapide ainsi.
Merci encore pour ta fonction!
Comment dupliquer un disque...ça vous intéresse?
Tutoriel et code source delphi ici
Et merci pour le peaufinement que tu y as apporté.
Partager