DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Pascal and Delphi (http://www.daniweb.com/forums/forum124.html)
-   -   Reading pascal file and searching for a particular word in that file (http://www.daniweb.com/forums/thread52166.html)

winpoorni Aug 10th, 2006 2:22 am
Reading pascal file and searching for a particular word in that file
 
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

Micheus Aug 14th, 2006 9:30 pm
Re: Reading pascal file and searching for a particular word in that file
 
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.


All times are GMT -4. The time now is 10:00 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC