0

Dear all,

im new to Delphi language.can u pls help me to do this one.
i want to read a particular file and searching for a particular word in that file..
pls assist me to do this one in delphi language

Thanx in advance..
Poornima.R

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Micheus
0

The procedure below must be able to count a particular word(SearchStr) occurence in a file(FileName).

procedure SearchingInFile(Filename, SearchStr :string);
var
  SearchPos,
  FoundCount :integer;
  FileLine :string;
  SearchFile :Text;
begin
  Assign(SearchFile, Filename);
  Reset(SearchFile);
  FoundCount := 0;
  while not EOF(SearchFile) do
  begin
 // the search will be no case sensitive (using UpCase)
    FileLine := UpCase(Readln(SearchFile));
    SearchPos := Pos(UpCase(SearchStr), FileLine);
    if SearchPos > 0 then
    begin
     // looking for more SearchStr into FileLine
      while SearchPos > 0 do
      begin
        // add 1 to founded word count
        Inc(FoundCount);
       // Advance SearchPos
        SearchPos := SearchPos +Length(SearchStr).
       // remove substring before last word founded, including the word
        FileLine := Copy(FileLine, SearchPos, Length(FileLine));
       // check for a new occurence of searched word on line readed
        SearchPos := Pos(UpCase(SearchStr), FileLine);
      end;
    end;
  end;
  Close(SearchFile);
end;

Is it? :rolleyes:
p.s.: I'm not sure about UpCase function in pascal (today, I'm Delphi programmer), but I think that have some thing similar.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.