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

Member Avatar for Micheus

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.